Search code examples
htmlperlmodulegentoo

HTML text field input can only accept 1 or 0 as a value in Perl


may i ask, how can HTML accept a text field input with only 1 or 0 as its value? Can anybody show me? I implement this HTML in my perl.

Here is the example of my code:

#!/usr/bin/perl
#index5.cgi

require 'foobar-lib.pl';
ui_print_header(undef, $module_info{'desc'}, "", undef, 1, 1);
ui_print_footer("/", $text{'index'});
#print "Content-type:text/html\n\n";
print qq~<html>
<link rel="stylesheet" type="text/css" href="style.css">
<body>

<div id="content">
<div id="bar">
<span><p>Data Gateaway</p></span>
</div>
<div id="main-container">
<table border="0" width="100%" height="100%">
<tr>
<td colspan="5">
<div id="button">
<form action="index.cgi">
<input type="submit" value="Back">
</form>
</div>
</td>
</tr>
<tr>
<td width="25%" align="left">
<div id="title"><span>BYPASS :</span></div>
</td>
<td width="20%">
<form method="POST" action="index7.cgi">
<td width="20%">
<table border="0" style=\"text-align:top;font-family:Arial, Helvetica, sans-serif;\" cellpadding="5">
<tr>
<td width="20%"><div id="data">Use either "1" or "0" to change value for <b>Basics Bypass</b>:</div></td>
</tr>
</table>
</td>
<td align="left" width="7%">
<table border="0" style=\"text-align:center;font-family:Arial, Helvetica, sans-serif;\" cellpadding="2">
<td>
<tr>
<td width="7%">
<input type="text" maxlength="1" name="voca" size="1">
</td>
</tr>
<tr>
<td width="7%">
<input type="text" maxlength="1" name="vocb" size="1">
</td>
</tr>
<tr>
<td width="7%">
<input type="text" maxlength="1" name="vocc" size="1">
</td>
</tr>
<tr>
<td width="7%">
<input type="text" maxlength="1" name="vocd" size="1">
</td>
</tr>
</td>
</table>
</td>
</tr>
<tr>
<td colspan="5">
<div id="description"><b>Description :</b></div>
</td>
</tr>
<tr>
<td colspan="5">
<div id="button">
<input type="submit" value="Change Default Settings"><input type="reset" value="Clear">
</form>
</div>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
~;

What function do i must add for that the text field input in the HTML only can accept the value of 1 or 0 only? Can anybody teach me how? Do i need to use if..else statement?


Solution

  • In your case I think its better to use dropbox with options of 1 and 0 in order to make it more intuitive. If you really want to use textbox, make input type=number, and every time that onchange event is being called, validate the current value and delete it if its invalid.