Solved

javascript: using RegExp to validate alphanumeric and length of characters

Posted on 2015-01-06
10
81 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 39
Reactjs with .NET 3 70
Html split(text) Part2 6 20
Put POST values into cookies. 14 32
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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…

763 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