Welcome to WebmasterWorld Guest from 35.173.234.237

Forum Moderators: open

Message Too Old, No Replies

Validation with a twist

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

Junior Member

10+ Year Member

joined:Mar 26, 2005
posts:148
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


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)
);
}
9:49 am on July 23, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 29, 2007
posts:228
votes: 0


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"

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

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


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

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

Junior Member

10+ Year Member

joined:Mar 26, 2005
posts:148
votes: 0


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 :-)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members