• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3362
  • Last Modified:

Alpha numeric reg-ex validation

Why doesn't this validate forany number of alpha numeric values?

var re = /[a-zA-Z0-9]*/ ;
return re.test(value);

Thanks.
0
kbach
Asked:
kbach
  • 3
  • 2
  • 2
  • +2
1 Solution
 
devicCommented:
show your any number of alpha numeric values
0
 
kbachAuthor Commented:
/^[a-zA-Z0-9]+$/

That worked.  I don't understand the significance of ^ and $.  Match at the beginning and end of the string?  Doesn't seem to work without it.

hmmm
0
 
devicCommented:
kbach, show where doesn't work

it works..

<script>
var mystring="123bbb555"
var re = /[a-zA-Z0-9]*/ ;
alert(re.test(mystring));
</script>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BurntSkyCommented:
@ kbach:
yes, ^ matches the beginning of a string and $ matches the end.  The reason you'd use it in this case is to avoid allowing unacceptable characters at the beginning or end of the string you're validating.  Without it the following would pass: "~123bbb555_", so the ^ means the string MUST start with the expression and the $ means the string MUST end with the expression.

Also, I'll confirm that devic's code does work, however, I still would recommend having the ^ and $ metacharacters in the expression.
0
 
ZylochCommented:
Yes, like everyone said you should use ^ and $ in order to match the whole string.

Another way to do this is to match something that should not occur to tell if it's totally alphanumeric or not. For instance:

<script>
var mystring="123bbb555**@#%"
var re = /[^a-zA-Z0-9]/ ;
alert(re.test(mystring));
</script>

The ^ at the beginning of the [] indicates a negative. If it sees any characters no included in a-zA-Z0-9, it returns true.
0
 
XxavierCommented:
Hi kbach,
    It is Xxavier here!! Devics code does not work cause it gives a +ve result for eg 123bbb555!@# , only an alpha-numeric substring needs to be found for a +ve result

<script>
var mystring="123bbb555!@#"
var re = /[a-zA-Z0-9]*/ ;
alert(re.test(mystring));
</script>

the ^ and $ forces it to be alpha-numeric from start to finish

<script>
var mystring="123bbb555!@#"
var re = /^[a-zA-Z0-9]*$/ ;
alert(re.test(mystring));
</script>

(We are going to have send Devic back to school again :-)

Xx :o)
0
 
BurntSkyCommented:
Actually, like I said, Devic's code does work; however, I suspect the results aren't what kbach wishes.  I presume he/she wishes, to match only alphanumeric values excluding special characters, which is why I recommended using the expression prefixed with the ^ and suffixed with the $ metacharacters.
0
 
devicCommented:
I got my mistake, I didn't see the word "validate".
people, there is no my code, it was example from kbach.
Xxavier, I don't leave the school, everyday something learn ;)

0
 
XxavierCommented:
Hi Mr D. !!
    I don't leave the school either I got kicked out, but you are right we must continue to learn for the rest of our lifes.

Xx :o)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now