Solved

alphanumeric  validation in regular expression in javascript

Posted on 2010-08-31
5
615 Views
Last Modified: 2012-05-10
Hi,

alphanumeric  validation in regular expression in javascript
0
Comment
  • 4
5 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 500 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:gurvinder372
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Website Home page blank 4 26
Autoplay video in mobile device 3 59
JavaScript error 1 33
Java Server Faces parameter pass? 6 20
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now