Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

618 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