?
Solved

alphanumeric  validation in regular expression in javascript

Posted on 2010-08-31
5
Medium Priority
?
637 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

Independent Software Vendors: 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!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

762 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