Checking a field for invalid chars

  • Follow


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 


0
Reply Astra 8/11/2005 9:53:21 AM

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
0
Reply William 8/11/2005 10:14:26 AM


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/

0
Reply Safalra 8/11/2005 10:17:12 AM

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" <SpamBlocked@tbdata.com> wrote in message 
news:op.svb9ecw7m9g4qz-wnt@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 


0
Reply Astra 8/11/2005 10:35:41 AM

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
0
Reply Dylan 8/11/2005 10:44:54 AM

Writing in news:alt.www.webmaster,comp.lang.javascript
 From the safety of the  cafeteria
Dylan Parry <usenet@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
0
Reply William 8/11/2005 11:07:17 AM

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" <usenet@dylanparry.com> wrote in message 
news:3m0ofaF14kaagU1@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 


0
Reply Astra 8/11/2005 11:28:33 AM

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
0
Reply Dylan 8/11/2005 12:01:17 PM

[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.
0
Reply Michael 8/11/2005 12:50:30 PM

shep loves to see invalid cars in a field

And rabbits too.
0
Reply robert 8/11/2005 12:57:44 PM

Michael Winter wrote

> [aww removed from follow-ups]

Why, are we not worthy?

-- 
Charles Sweeney
http://CharlesSweeney.com
0
Reply Charles 8/11/2005 1:38:42 PM

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
0
Reply Mick 8/11/2005 4:22:04 PM

11 Replies
206 Views

(page loaded in 0.633 seconds)

Similiar Articles:


















7/24/2012 4:56:34 PM


Reply: