Solved

How to check if a 7 character string contains one letter and six numbers?

Posted on 2013-06-17
5
286 Views
Last Modified: 2013-06-17
Experts,

I need to adjust the following script to check to see if the variable being passed contains:

- 1 regular character (letter a-z, capitalization is not important)

and

- 6 numbers

I've tried several variations of what I though should work but, am just getting frustrated at this point.

Cheers!

var x=document.forms["step1"]["alt_user"].value;
if((x.length =="7" && CHECK FOR LETTER AND ^ NUMBERS HERE) || (x == null || x == "")){}

Open in new window

0
Comment
Question by:evibesmusic
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39253936
How about a regex - this pattern
(?:^[a-zA-Z]{1}[0-9]{6})

Checks if first character a letter and then 6 numbers - wasn't clear from your question the makeup of the string.
If it doesn't matter the order just remove the ^

http://jsfiddle.net/wNDCh/1/
0
 

Author Comment

by:evibesmusic
ID: 39253993
@GaryC123

Your assumption was spot on. Sorry for not making that more clear.

The string being passes should be in the following format:

x123456

To implement your suggestion would my code then look like this?

var x=document.forms["step1"]["alt_user"].value;
if((x.length =="7" && x==(?:^[a-zA-Z]{1}[0-9]{6})) || (x == null || x == "")){}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39254010
check the jsfiddle link
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39254051
var x=document.forms["step1"]["alt_user"].value;
var regPattern=/(^[a-zA-Z]{1}[0-9]{6})/

if (x.search(regPattern)==-1){ //no match
    // code for no match
}
else{ // matched
    // code here for match
}

Open in new window


This doesn't take account for accented characters etc...
0
 

Author Closing Comment

by:evibesmusic
ID: 39254080
Perfect. Thank you very much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

920 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