Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-06-17
5
Medium Priority
?
295 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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

810 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