?
Solved

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

Posted on 2013-06-17
5
Medium Priority
?
291 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
[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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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

762 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