Dummy needs a simple problem solved.

B

Blue®

I found this piece of JS which serves exactly what I need. It validates if
the input is exactly five numberic characters.

When it is validated, the form action is not carried out. What should I add
to the code so that the form action is carried out?

P/s: I do not know JS at all.

==================================
<html>
<head>
</head>
<body>

<script language="JavaScript1.2">
function checkpostal(){
var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
if (document.myform.RecordID.value.search(re5digit)==-1) //if match failed
alert("Please enter a valid 5 digit number.")
}
</script>

<form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">
<input type="text" name="RecordID" size=15>
<input type="button" onClick="checkpostal()" value="check">
</form>
</body>
</html>
 
L

Lasse Reichstein Nielsen

Blue® said:
I found this piece of JS which serves exactly what I need. It validates if
the input is exactly five numberic characters.

When it is validated, the form action is not carried out. What should I add
to the code so that the form action is carried out?
P/s: I do not know JS at all.

Or HTML?
Change <input type="button" ...> to <input type="submit" ...>

However, then it will always submit. The "checkpostal" function
doesn't return any value, so there is no way for the rest of the
page to know whether the validation succeeded.

Try this:

Remeber to add a said:
<html>
<head>
</head>
<body>

<script language="JavaScript1.2">

In HTML 4 and later, the "type" attribute is required. The "language"
attribute is deprecated, and should be omitted. If it is included,
don't use the version number 1.2 unless you know what the difference
between 1.2 and 1.3 is, and in which browsers it makes a difference
(i.e., just don't).
<script type="text/javascript">

function checkpostal(){
var re5digit=/^\d{5}$/; //regular expression defining a 5 digit number
if (document.myform.RecordID.value.search(re5digit)==-1) {//if match failed
alert("Please enter a valid 5 digit number.");
return false;
}
return true;
}
</script>

<form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">

It is smarter to add validation to the submit event of the form, then
it is checked no matter how the form is submitted.

<input type="text" name="RecordID" size=15>
<input type="button" onClick="checkpostal()" value="check">

Just make it a submit button then:
</form>
</body>
</html>

Good luck.
/L
 
B

Blue®

Thanks, Lasse. It works!!!

I spent the whole morning for it. Luckily you stepped in. The final code is
as below:

==================================
Lawrence Lam
Keywords: Validate JS Javascripts input form digit digits number character
characters
==================================

<script type="text/javascript">

function checkpostal(){
var re6digit=/^\d{6}$/; //regular expression defining a 6 digit number
if (document.myform.RecordID.value.search(re6digit)==-1) {//if match
failed
alert("Please enter a valid 6-digit number.");
return false;
}
return true;
}
</script>


<FORM name=myform ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST"
onsubmit="return checkpostal()">
<b>Quick Machine Search<br>
</b>Please enter <i>Record ID</i>:<br>
<input type=text name="RecordID" size="16"><font color="#808080"> <br>
</font>
<input type=submit value="Search Machine"><hr color="#FF0000"
size="1"></form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top