Solved

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

Posted on 2013-06-17
5
285 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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

705 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

18 Experts available now in Live!

Get 1:1 Help Now