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
Solved

javascript: using RegExp to validate alphanumeric and length of characters

Posted on 2015-01-06
10
79 Views
Last Modified: 2015-01-07
How can i use regexp to validate whether a string is alphanumeric and of 5 characters in length?


i was doing the following

var regexp= /[^A-Za-z0-9]+/;
if ( ! regexp.test(strLocation) )
      throw "Only alpha-numeric characters are permitted";
0
Comment
Question by:badtz7229
  • 4
  • 4
  • 2
10 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533788
Like this:
var regexp= /^[A-Za-z0-9]{5}$/;

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533793
Actually a shorter version would be:
var regexp= /^[a-z0-9]{5}$/i;

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40533797
I haven't tested but maybe this:

^([a-zA-Z0-9]){1,5}$

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40533799
Oh ok, 5 chars, then {5} instead of {1,5}
0
 

Author Comment

by:badtz7229
ID: 40533893
and i tested as:

var strLocation = SERV**123
var regexp =    /^[A-Za-z0-9]{5}$/i;
if ( regexp.test(strLocation) )
      throw "Only alpha-numeric characters are permitted";
                  
                  
but no error was thrown.

if i use      var regexp =      /[^A-Za-z0-9]+/;                  then indeed an error is thrown.
0
 

Author Comment

by:badtz7229
ID: 40533905
oh, i hadn't realized. i forgot to put the "!" in the if statement.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533914
yep :)
0
 

Author Comment

by:badtz7229
ID: 40534167
how do i permit spaces as a character  as well in this reg expression?
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40534224
Just add a space to the end of that sequence:
var regexp = /^[a-z0-9 ]{5}$/i;

Open in new window

Keep it like this. As it has the i at the end, this regex is case insensitive, no need for the uppercase letters.
0
 

Author Closing Comment

by:badtz7229
ID: 40535662
thank you. this worked for  me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

860 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