Connecting Tech Pros Worldwide Help | Site Map

Dummy needs a simple problem solved.

BlueŽ
Guest
 
Posts: n/a
#1: Jul 20 '05
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>


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Dummy needs a simple problem solved.


"BlueŽ" <superbaby@myjaring.net> writes:
[color=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?[/color]
[color=blue]
> P/s: I do not know JS at all.[/color]

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 <!DOCTYPE> declaration. It is required for HTML 4.[color=blue]
> <html>
> <head>
> </head>
> <body>
>
> <script language="JavaScript1.2">[/color]

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;
}[color=blue]
> </script>
>
> <form name="myform" ACTION="cgi-bin/dbase/dbase.cgi" METHOD="POST">[/color]

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

<form .... onsubmite="return checkpostal()">
[color=blue]
> <input type="text" name="RecordID" size=15>
> <input type="button" onClick="checkpostal()" value="check">[/color]

Just make it a submit button then:
<input type="submit" value="check">
[color=blue]
> </form>
> </body>
> </html>[/color]

Good luck.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
BlueŽ
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Dummy needs a simple problem solved.


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>


Closed Thread