...

View Full Version : numeric characters only in form field



dan deuce
10-03-2011, 09:34 PM
Hi guys,

This has been asked before, and i've seen many of the previous posts. And tried for hours. But finally i've broken and have to ask for help.

What am I doing wrong here? Can someone please explain?



var u=document.forms["lead"]["name"].value;
var v=document.forms["lead"]["city"].value;
var x=document.forms["lead"]["country"].value;
var y=document.forms["lead"]["mobile"].value;
var z=document.forms["lead"]["email"].value;

if (u==null || u=="")
{
alert("Please enter your first name.");
return false;
}

if (v==null || v=="")
{
alert("Please enter your city.");
return false;
}

if (isNaN(lead.y.value))
{
alert("bloody work!");
return false;
}

if (x=="Argentina" && y.length < 11)
{
alert("Please enter a valid Argentinian phone number.");
return false;
}


Thanks in advance.

Old Pedant
10-03-2011, 09:46 PM
if (isNaN(y))
{
alert("bloody work!");
return false;
}

Though that is rather inflexible. Don't people in Argentina ever enter spaces or dashes in phone numbers? Such as 337-111 88821?? (Or various other formats.)

I would do this:


var numonly = y.replace(/[^\d]/g, "" );
if (x=="Argentina" && numonly.length < 11)
{
alert("Please enter a valid Argentinian phone number.");
return false;
}
and forget the other test.

Old Pedant
10-03-2011, 09:49 PM
And it's minor, but you are working too hard.

In place of

var u=document.forms["lead"]["name"].value;
var v=document.forms["lead"]["city"].value;
var x=document.forms["lead"]["country"].value;
var y=document.forms["lead"]["mobile"].value;
var z=document.forms["lead"]["email"].value;

try simply


var form = document.lead;
var u = form.name.value;
var v = form.city.value;
var x = form.country.value;
var y = form.mobile.value;
var z = form.email.value;

though, personally, I wouldn't use names such as x,y,z,etc.

What's wrong with


var form = document.lead;
var name = form.name.value;
var city = form.city.value;
var country = form.country.value;
var mobile = form.mobile.value;
var email = form.email.value;

??? So that your code is self-documenting and self-explanatory?

dan deuce
10-04-2011, 12:52 PM
Thanks so much for your help.

I'm keen to understand and learn the code. Do you mind explaining how this line works please?



var numonly = y.replace(/[^\d]/g, "" );

devnull69
10-04-2011, 01:21 PM
This is the replace method whose first parameter is a so-called "regular expression". This is quite sophisticated stuff

General explanation:
^\d means: non-digit characters
/.../g means: globally

So this regex matches all non-digit characters in the string y and replaces them with "" (so removes them)

Philip M
10-04-2011, 03:47 PM
One more time, form validation of the pattern if (u=="") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum