Question

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?

Was it helpful?

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.

OTHER TIPS

To make 0 and 1 the only accepted input, add a pattern attribute:

pattern="0|1"

But (in addition to lack of support in old browsers) this does not prevent the user from typing something else. It just means that upon form submission, the data is checked and and an error message is issued if the check does not pass. Moreover, an empty string will pass, too, unless you also use the attribute required.

To actually prevent the user from type anything but 0 or 1, you would need to use a small piece of JavaScript.

In general, checkboxes are usually preferable when there are only two values that you wish to accept.

And all such methods are just for helping the user, not for security. Your server-side code should check the data before doing anything with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top