Solved

javascript: using RegExp to validate alphanumeric and length of characters

Posted on 2015-01-06
10
78 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

776 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