Forum Moderators: open

Message Too Old, No Replies

Need help restricting input

         

Acternaweb

1:21 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



I am trying to restrict input on this field to be only alpha, no number but not having any luck.
Any help would be much appreciated.

if ( document.feedbackform.name.value == "" )
{
alert ( "Sorry, you left name field empty. Please Enter your First and Last Name." );
document.feedbackform.name.focus();return false;
}

SpaceFrog

1:27 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



if(isNan(document.feedbackform.name.value))...

Rambo Tribble

1:31 pm on Apr 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try:

~.value.search(/\d/)!=-1

as your test condition.

Acternaweb

1:42 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



I get a runtime error when I use
if(isNan(document.feedbackform.name.value ==""))

Not sure what ~.value.search(/\d/)!=-1 means or where to put it

SpaceFrog

6:29 am on Apr 13, 2005 (gmt 0)

10+ Year Member



why did you add =="" in isNaN?

Bernard Marx

7:24 am on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



isNan gives an error because the function is isNaN.

..but it's no so good anyway. It will accept "I am 1337", which isn't fully alpha.

Rambo's use of a regular expression is a little better suited.

It uses a regular expression literal (the forward slashes denote an RE).
\d means 'a digit'.
The ~ symbol is Rambo's way of saying 'and all that'.

It tests for the presence of digits, and rejects any strings that contain at least one digit. Hmm. This still allows characters such as @, &, #

How about:

if(/^[a-z]+$/i.test(document.feedbackform.name.value))

This also rejects no input, but comes with the same caveats as the last time I suggested it to you:
[webmasterworld.com...]