Forum Moderators: open

Message Too Old, No Replies

Validation with a twist

         

ramoneguru

10:41 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



Ok, I have the validation functions and they seem to work. Only problem is when there is an error the form gets reset and it looks like its being resubmitted even though I have the correct "onSubmit=return validate(this)" why is my form being rest and resubmitted?

This page is saved as "hello.html" and I need it to be submitted to "accept.lasso" when the form is valid.....I'm not sure how to do that part.

<html>
<head>
<script type="text/javascript">
function checkRIC(value)
{
var pattern = /[0][1-7]/
var result = value.match(pattern)

if (result == null) {
alert("The RIC: " + value + " was not Valid! Result was: " + result)
return false
}
}

function checkName(name)
{
var pattern = /^[a-zA-Z]{2,}$/
var result = name.match(pattern)

if (result == null) {
alert("The name: " + name + " was not Valid! Result was: " + result)
return false
}
}

function validate(form)
{
checkName(form.name_first.value)
checkName(form.name_last.value)
checkRIC(form.ric.value)
}

</script>
</head>
<body>

<form action="hello.html" name="form" id="form" method="post" enctype="text/plain" onSubmit="return validate(this)">
<table>
<tr>
<th>First name</th>
<td><input type="text" id="name_first" name="name_first"/></td>
</tr>

<tr>
<th>Last name</th>
<td><input type="text" id="name_last" name="name_last"/></td>
</tr>
<tr>
<th>RIC</th>
<td><select name="ric">
<option value="" selected="selected">Choose RIC...</option>
<option value="01">Alameda</option>
<option value="02">Butte</option>
<option value="03">Imperial</option>
<option value="04">Los Angeles</option>
<option value="05">Sacramento</option>
<option value="06">San Diego</option>
<option value="07">San Joaquin</option>
</select></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="-add" value="Login" /></td>
</tr>
</table>
</form>

</body>
</html>

Little help please...

--Nick

Bernard Marx

11:00 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The form is submitted regardless because there is no return value from the
validate
function.


function validate(form)
{
return (
checkName(form.name_first.value)
&& checkName(form.name_last.value)
&& checkRIC(form.ric.value)
);
}

Drag_Racer

9:49 am on Jul 23, 2007 (gmt 0)

10+ Year Member



the reason the form appears to reset is the the value of the 'action' attribute of the form tag is set to the same as the ducument containing the form. It is just calling the original page again, not resetting the form. Though the effect is the same.

if you want the form to submit to "accept.lasso", then set action="accept.lasso"

dreamcatcher

1:59 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don`t rely on JS alone, make sure you have server side validation too. Easy for someone to disable javascript to submit your form.

dc

ramoneguru

10:25 pm on Jul 23, 2007 (gmt 0)

10+ Year Member



Thanks those appear to work. Yeah, this is just one layer of validation we still check on server side as well :-) thanks for the input :-)