Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

alphanumeric  validation in regular expression in javascript

Posted on 2010-08-31
5
Medium Priority
?
648 Views
Last Modified: 2012-05-10
Hi,

alphanumeric  validation in regular expression in javascript
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 2000 total points
ID: 33569686
The attached isAlphaNum is probably what you need.
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
  function isAlphaNum(s)
  {
    return /([a-zA-Z0-9]+)$/.test(s);
  }

  document.write('<pre>\n');

  s = "123ABC";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '<br>\n');
  s = "foo-";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '<br>\n');
  s = "bar.";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '<br>\n');
  document.write('</pre>\n');
</SCRIPT>
</HEAD>

Open in new window

0
 
LVL 6

Expert Comment

by:apresence
ID: 33569692
Save that to an htm/html file and look at the output:
123ABC is alphanum = true
foo- is alphanum = false
bar. is alphanum = false
0
 
LVL 6

Expert Comment

by:apresence
ID: 33569718
Sorry, please use the following instead.

Output:
123ABC is alphanum = true
foo- is alphanum = false
bar. is alphanum = false
.baz is alphanum = false

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
  function isAlphaNum(s)
  {
    return /^([a-zA-Z0-9]+)$/.test(s);
  }

  document.write('<pre>\n');

  s = "123ABC";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '\n');
  s = "foo-";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '\n');
  s = "bar.";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '\n');
  s = ".baz";
  document.write(s + ' is alphanum = ' + isAlphaNum(s) + '\n');
  document.write('</pre>\n');
</SCRIPT>
</HEAD>

Open in new window

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33569741
var str = "sdfsdfsdf234234aDSD";
var re = new RegExp("^[a-zA-Z0-9]+$", "g");
var myArray = re.test(str);
0
 
LVL 6

Expert Comment

by:apresence
ID: 33569772
dravidnsr, please be sure to use the second regular expression I provided.  The first will match strings which only end in alphanumeric chars, like:
.foo

The second one fixes this by adding "^" to the regular expression, which additionally tells the regex parser that the first character onward has to be alphanumeric.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question