Solved

alphanumeric  validation in regular expression in javascript

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

832 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