Solved

alphanumeric  validation in regular expression in javascript

Posted on 2010-08-31
5
624 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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…

726 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