Forum Moderators: open

Message Too Old, No Replies

need help

         

cb153

2:41 pm on Jul 30, 2010 (gmt 0)

10+ Year Member



I am new to javasacript and need some help please.

How do I change the code so that it checks if the postcode is not a number, using the isNaN function?


//function to check fields
function Blank_TextField_Validator(form)
{

// check empty name field
if (form.Name.value == "")
{
alert("Please fill in the Name field");
form.Name.focus();
return (false);
}

//check empty address field
if (form.Address.value == "")
{
alert("Please fill in the Address field");
form.Address.focus();
return (false);
}

//check empty suburb field
if (form.Suburb.value == "")
{
alert("Please fill in the Suburb field");
form.Suburb.focus();
return (false);
}

//check empty postcode field
if (form.Postcode.value == "")
{
alert("Please fill in the Postcode field");
form.Postcode.focus();
return (false);
}

//check postcode four number length
if (form.Postcode.value.length !=4)
{
alert("Your Postcode must contain four numbers.");
form.Postcode.focus();
return (false);
}

return (true);
}

Fotiman

3:15 pm on Jul 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would probably just use a RegExp.

var postalRegEx = /^\d{5}$/; // 5 digits only
if (!postalRegEx.test(form.Postcode.value)) {
// Invalid
}


Note, that particular one checks for 5 digits (as in a US postal code)... it looks like you're looking for 4 digits, so just change the 5 to a 4.

cb153

3:54 pm on Jul 30, 2010 (gmt 0)

10+ Year Member



Thanks, but I specifically need to use isNaN, but I cant get it to work.

Fotiman

4:54 pm on Jul 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What have you tried?

if (isNaN(form.Postcode.value)) }
// not a number
}

cb153

5:06 pm on Jul 30, 2010 (gmt 0)

10+ Year Member



Thanks for that, I had left out the "value".