473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

match exactly 4 numbers

I am checking user input in a form and they should ennter only the
last 4 digits of their account number. My code catches it if they
enter 3 but it thinks it ok if they enter 5. How can I have it fail
for 5 numbers?

function checkAccount(account) {

var regexp = /\d{4}/;

if (!regexp.test(account)){
alert ('Please enter the last 4 digits of your account number.');
return false;
}

else{
return true;
}

}

Feb 26 '07 #1
4 2322
On Feb 26, 6:23 pm, "jimmygoogle" <jtbutle...@comcast.netwrote:
I am checking user input in a form and they should ennter only the
last 4 digits of their account number. My code catches it if they
enter 3 but it thinks it ok if they enter 5. How can I have it fail
for 5 numbers?

function checkAccount(account) {

var regexp = /\d{4}/;

if (!regexp.test(account)){
alert ('Please enter the last 4 digits of your account number.');
return false;
}

else{
return true;
}

}
Try surrounding your regular expression with '^' and '$' characters.
In classic regular expressions, they mean "start of line" and "end of
line", respectively. In your case, you want to find the four digits in
the test-string, and they are successfully found if the user types 5
digits; what you wanted to say is "I want ONLY four digits", and I
think (haven't tried in JS, though) this can be accomplished by /^
\d{4}$/ Maybe the best way is to put maxlength=4 in your <inputtag,
thus avoiding one part of the unpleasant alert-boxes.

Darko

Feb 26 '07 #2
Lee
jimmygoogle said:
>
I am checking user input in a form and they should ennter only the
last 4 digits of their account number. My code catches it if they
enter 3 but it thinks it ok if they enter 5. How can I have it fail
for 5 numbers?

function checkAccount(account) {

var regexp = /\d{4}/;
That will match any entry that contains 4 consecutive digits, no matter
what other characters there may be.
You want to match the beginning of the string is followed by
exactly 4 digits, followed by the end of the string:

var regexp = /^\d{4}$/;
--

Feb 26 '07 #3
thanks guys it works now.

Feb 26 '07 #4
jimmygoogle wrote:
I am checking user input in a form and they should ennter only the
last 4 digits of their account number. My code catches it if they
enter 3 but it thinks it ok if they enter 5. How can I have it fail
for 5 numbers?


function checkAccount(account) {
if /^\d{4}$/.test(account) return true;
alert('Please enter the last 4 digits of your account number.');
return false;
}

Mick
>
function checkAccount(account) {

var regexp = /\d{4}/;

if (!regexp.test(account)){
alert ('Please enter the last 4 digits of your account number.');
return false;
}

else{
return true;
}

}
Feb 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bdwise | last post by:
I have this in my body tag: something();something(); document.thisForm.textBox1.focus();something(); And I want to find a part between the semicolons that ends in focus() and remove the...
58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
3
by: Rich | last post by:
I am using Excel 97 and Access 97. I have two memory variables that are doubles in Access. I did the following math in Access. $28,721,211.86 / $4,263,630,151.41 In Access the answer is...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
3
by: skosmicki | last post by:
I need to create an function similar to the "MATCH" function in Excel that evaluates a number within a set of numbers and returns whether there is a match. I have put the example of what I see in...
3
by: sapsi | last post by:
Hi, I managed to create a python class and instantiate that from my Objective C Cocoa App (its not a python app). Essentially, i made two classes in IB and then another class(ogle) with outlets...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
1
by: socc16e | last post by:
I have an excel sheet that I have imported into SQL 2000 and I need to match the values in one to the values in another and have them output the matching codes to a separate table or file. After...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.