Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation 1

Status
Not open for further replies.

timcarr

Programmer
Jan 10, 2001
23
0
0
ZA
I need validate that a form field does not begin with a number.
In Perl this would be something like:
variable =~ /^\d/

The code will be similar to this:

if ([field begins with number code])
{
alert("Field cannot begin with a number.");
theForm.field.focus();
return (false);
}


Any help is appreciated.
Thanks
 
How about the following,

if isNaN(variable)
{}
else
{
alert("Field cannot begin with a number.")
...
...
...et cetera
}

Works for me..... DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
Thanks for the reply, but since this is a password field I would like numbers to be allowed, just not at the beginning (our server doesn't allow this).
 
ok, so how about....

if isNaN(form.field.value.slice(0,1))
{}
else
{}
et cetera


this will take the first digit of the password and check it is not a number, but not care less about the other letters.

Better??
DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
That's exactly what I needed.

Thanks.
 
Anytime. DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
timcarr,

regular expressions in javascript are based on perls implementation of regular expressions. to use regexps in js to solve your problem, try:

if(/^\d/.test(somevariable))
{
alert("The variable starts with a number.")
}
else
{
alert("The variable does not start with a number.")
}

jaredn@eae.net -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top