473,378 Members | 1,066 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,378 software developers and data experts.

match entire string (regular expression & javascript)

Hello, I'm trying to perform a very simple validation of user input. I
want to verify that the user entered a six-digit string consisting
entirely of numbers. So anything from 000000 to 999999 is considered
valid. The problem that I'm having is getting the validation to work on
the entire string. In other words, 000000 is okay but 000000000000 is
also returning as a match. Here's a quick code block...I have something
along these lines....

/************************************************** **/
<html>
<body>
<INPUT name="txtNumberField" type="text" id="txtNumberField">
<INPUT onClick="fnTestNumberField()" id=Button1 type=button value="Test
Number!" name=btnTest>
<script language=javascript>
function fnTestNumberField()
{
var sNumberValue = document.getElementById("txtNumberField").value;

if (sNumberValue.match(/\A[0-9]{6}\z/))
{
alert("match");
} else {
alert("no match");
}
}
</script>
</body>
</html>
/************************************************** **/

That is failing when I enter 123456 into the textbox. Why, though? I
know I can replace...

if (sNumberValue.match(/\A[0-9]{6}\z/))

....with something like...

if (sNumberValue.length == 6 && sNumberValue.match(/[0-9]{6}/))

....or I could assign a maxlength to the input box, of course. The thing
is, I really want to know WHY the regular expression isn't responding
as I'd expect. Is there a syntax error somewhere in the code?

Any suggestions would be appreciated. Thanks!

Jul 23 '05 #1
2 2603

GreggTB wrote:
Hello, I'm trying to perform a very simple validation of user input. I
want to verify that the user entered a six-digit string consisting
entirely of numbers. So anything from 000000 to 999999 is considered
valid. The problem that I'm having is getting the validation to work on
the entire string. In other words, 000000 is okay but 000000000000 is
also returning as a match. Here's a quick code block...I have something
along these lines....

/************************************************** **/
....
if (sNumberValue.match(/\A[0-9]{6}\z/))
{
alert("match");
} else {
alert("no match");
}
}

Use ^ to match the beginning of the string and $ to match the end.
Also, \d is the same as [0-9].
/^[0-9]{6}$/ or /^\d{6}$/

Both work.

Jul 23 '05 #2
Excellent! I knew there had to be a way to do that but I'm (obviously)
really new to regular expressions. Thank you so much!

Jul 23 '05 #3

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

Similar topics

9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
1
by: Venkat | last post by:
Hi, I am using match function of string to find if a character is there in a string. The function Match is working fine with all the other characters except when the searching character is "+"....
4
by: aliensite | last post by:
My code is too greedy, how can it be fixed? Here is my code: Desired output - First:,Second:,Third: <br> <script type="text/javascript"> var regEx = /*?:/g; var html = "<br>First:ratio<br...
2
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be...
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
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...
4
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that...
1
by: sreemathy2000 | last post by:
My requirement is to read/write a javascript file in a windows applications. this javascript file is used by my website.. function test { var a; //start dim obj ={'abc','bcd','cde','def'};...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.