Solved

javascript: using RegExp to validate alphanumeric and length of characters

Posted on 2015-01-06
10
76 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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

746 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

12 Experts available now in Live!

Get 1:1 Help Now