...

View Full Version : Newbie JS question.



Pyraine
05-26-2009, 06:38 PM
Hi, I'm just trying to validate a form and it just isn't working. I'm pretty new to this, but here is my code so far:


<script language="JavaScript" type="text/javascript">
<!--
function validation ( form )
{

if (form.firstname.value == "") {
alert( "Please enter a first name." );
form.firstname.focus();
return false ;
}


if (form.lastname.value == "") {
alert( "Please enter a last name." );
form.lastname.focus();
return false ;
}


if (form.firstname.value == "`1234567890-=[]'#/.,\!"$%^&*()_+{}@~?><|") {
alert( "Please enter a valid first name." );
form.firstname.focus();
return false ;
}


if (form.lastname.value == "`1234567890-=[]'#/.,\!"$%^&*()_+{}@~?><|") {
alert( "Please enter a valid last name." );
form.lastname.focus();
return false ;
}

return true;
}
//-->
</script>

this does not work at all, however if i only do one validation on field such as:


<script language="JavaScript" type="text/javascript">
<!--
function validation ( form )
{

if (form.firstname.value == "") {
alert( "Please enter a first name." );
form.firstname.focus();
return false ;
}

}
//-->
</script>

Then it works fine.

Thanks guys.

Philip M
05-26-2009, 06:51 PM
Validation of the form if (form.firstname.value == "") { is barely worthy of the name, as even a single space or a ? will return false (i.e. pass the validation).

A proper name may only contain letters a-z, and perhaps a hyphen (Mary-Lou), a space (Mary Lou) or an apostrophe (O'Reilly).

Follow this example:-




var fn = document.formname.firstname.value;
fn = fn.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces;
if (/[^a-z\-\'\s]/gi.test(fn)) {
alert ("Please enter a valid first name (only A-Z hyphen space and apostrophe allowed)");
document.formname.firstname.value = ""; // erase invalid entry
document.formname.firstname.focus();
return false;
}


<script language="JavaScript"> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. Likewise the <!-- and //--> tags have not been needed since IE4.


Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.



Quizmaster: In which book is Room 101 a place to be feared?
Contestant: 101 Dalmations

Pyraine
05-26-2009, 07:32 PM
Hi, thanks for the help.

Sorry about the title, I assumed the tags were for helping people find the post.

The validation of the form if (form.firstname.value == "") { was intended to be used alongside other validation, just to make sure that the form was not accidentally or intentionally skipped when it was required. I noticed with the validation you gave me it would pass validation if the field was left blank.

also I had some issues with document.formname.firstname, I tried replacing it with form.firstname and that seemed to do the trick.

Thanks for your help though! Using your technique it appears I can validate multiple fields on my form, which is where I was struggling before. Thank you very much.

Also thanks for informing me of the obsolete code I was using.

Philip M
05-26-2009, 08:40 PM
. I noticed with the validation you gave me it would pass validation if the field was left blank.



Sorry, my bad. You can specify a minimum length for a field as follows:-

if ((fn.length < 2) || (/[^a-z\-\'\s]/gi.test(fn))) {

Old Pedant
05-26-2009, 09:19 PM
You could be a little less flexible (that RE will, for example, *pass* the name "- - - ' ' -") with maybe


if ( ! ( /^[a-z]+([\-\'\s]?[a-z]+)+$/i.test(fn) ) ) {

That says "at least one letter followed by an optional delimiter of dash, apostrophe or space followed by at least one more letter". The "delimiter with letter(s) following" can be repeated. Note that this does allow for as simple a name as two letters.

Pyraine
05-26-2009, 09:48 PM
Thanks guys I appreciate the help

Philip M
05-27-2009, 08:11 AM
You could be a little less flexible (that RE will, for example, *pass* the name "- - - ' ' -") with maybe


if ( ! ( /^[a-z]+([\-\'\s]?[a-z]+)+$/i.test(fn) ) ) {

That says "at least one letter followed by an optional delimiter of dash, apostrophe or space followed by at least one more letter". The "delimiter with letter(s) following" can be repeated. Note that this does allow for as simple a name as two letters.

Yes, but it will also pass zxz'xzx or something equally silly. How is that better than - - - ' ' - ?

If you want to reduce the scope for idiocy further you can specify that the name field contains at least one vowel and one consonant. (Al, Jo, Lee, Byng). But at the end of the day you cannot stop people entering their names as Xuzyzyzyz or Mickey Mouse. Or of course mis-spelling their name as Phlip or whatever.

Old Pedant
05-27-2009, 08:22 AM
Yeah, too true. Okay, just need to protect against entering all spaces and entering HTML, I guess.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum