By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,249 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,249 IT Pros & Developers. It's quick & easy.

Checking a field for invalid chars

P: n/a
Hi All

Is there anyway that before I submit a form I can check the contents of an
input field to see that it only contains letters and numbers.

Thanks

Robbie
Aug 11 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Writing in news:alt.www.webmaster,comp.lang.javascript
From the safety of the Posted via Supernews, http://www.supernews.com
cafeteria
Astra <No@Spam.com> said:
Hi All
How do you do?
Is there anyway that before I submit a form I can check the contents of
an
input field to see that it only contains letters and numbers.


yes - usual caveats apply.

What you want is a regular expression.

--
William Tasso

** Business as usual
Aug 11 '05 #2

P: n/a
Astra wrote:
Is there anyway that before I submit a form I can check the contents of an
input field to see that it only contains letters and numbers.


Why did you start two threads on this subject? I've replied in the one
titled 'Stopping user from inputting anything other than letters and
numbers'.

--
Safalra (Stephen Morley)
http://www.safalra.com/programming/javascript/

Aug 11 '05 #3

P: n/a
Hi William

I just can't find the Reg Exp syntax for just letters and numbers, eg A-Z,
a-z and 0-9.

Do you know what it is?

Regards

Robbie
"William Tasso" <Sp*********@tbdata.com> wrote in message
news:op*******************@tbdata.com...
Writing in news:alt.www.webmaster,comp.lang.javascript
From the safety of the Posted via Supernews, http://www.supernews.com
cafeteria
Astra <No@Spam.com> said:
Hi All
How do you do?
Is there anyway that before I submit a form I can check the contents of
an
input field to see that it only contains letters and numbers.


yes - usual caveats apply.

What you want is a regular expression.

--
William Tasso

** Business as usual
Aug 11 '05 #4

P: n/a
Using a pointed stick and pebbles, Astra scraped:
I just can't find the Reg Exp syntax for just letters and numbers, eg A-Z,
a-z and 0-9.


[A-Za-z0-9]*

This will match zero or more letters and numbers. Replacing the * with a
+ will require one or more characters.

Of course, this will not match non-Latin letters or space characters
either, so you may have to fiddle with it if you require the ability to
use those.

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Aug 11 '05 #5

P: n/a
Writing in news:alt.www.webmaster,comp.lang.javascript
From the safety of the cafeteria
Dylan Parry <us****@dylanparry.com> said:
Using a pointed stick and pebbles, Astra scraped:
I just can't find the Reg Exp syntax for just letters and numbers, eg
A-Z, a-z and 0-9.


[A-Za-z0-9]*

This will match zero or more letters and numbers. Replacing the * with a
+ will require one or more characters.

Of course, this will not match non-Latin letters or space characters
either, so you may have to fiddle with it if you require the ability to
use those.


This may assist the o/p: http://www.regular-expressions.info/

--
William Tasso

** Business as usual
Aug 11 '05 #6

P: n/a
Hi Dylan

I used your example in my code:

if (!([A-Za-z0-9]*.test(document.form1.fred.value)))
{
......
}

but it generates a syntax error.

Do you know what I've done wrong?

Rgds Robbie
"Dylan Parry" <us****@dylanparry.com> wrote in message
news:3m*************@individual.net...
Using a pointed stick and pebbles, Astra scraped:
I just can't find the Reg Exp syntax for just letters and numbers, eg A-Z,
a-z and 0-9.


[A-Za-z0-9]*

This will match zero or more letters and numbers. Replacing the * with a
+ will require one or more characters.

Of course, this will not match non-Latin letters or space characters
either, so you may have to fiddle with it if you require the ability to
use those.

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Aug 11 '05 #7

P: n/a
Using a pointed stick and pebbles, Astra scraped:

<snip code />
but it generates a syntax error.

Do you know what I've done wrong?


Try:

var re = new RegExp("^[A-Za-z0-9]*$");
if (document.form1.fred.value.match(re)) {
alert("Okay");
}
else {
alert("Not Okay");
}

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Aug 11 '05 #8

P: n/a
[aww removed from follow-ups]

On 11/08/2005 13:01, Dylan Parry wrote:

[snip]
var re = new RegExp("^[A-Za-z0-9]*$");
if (document.form1.fred.value.match(re)) {


I'd prefer:

var elem = document.forms.form1.elements;

if(!/^[A-Za-z0-9]*$/.test(elem.fred.value)) {
/* Bad */
}

Or perhaps:

if(!/^[^\W_]*$/.test(...)) {
/* Bad */
}

which is the same thing.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 11 '05 #9

P: n/a
shep loves to see invalid cars in a field

And rabbits too.
Aug 11 '05 #10

P: n/a
Michael Winter wrote
[aww removed from follow-ups]


Why, are we not worthy?

--
Charles Sweeney
http://CharlesSweeney.com
Aug 11 '05 #11

P: n/a
Astra wrote:
Hi Dylan

I used your example in my code:

if (!([A-Za-z0-9]*.test(document.form1.fred.value)))
{
.....
}

but it generates a syntax error.

Do you know what I've done wrong?


if (!/[a-z0-9]+/i.test(document.form1.fred.value))

Mick
Aug 11 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.