![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
Hello All,
I am rather new to regular expressions. I have this regualr expression to check if the number entered is between 1 and 366: var regVal= new RegularExp("^[1-366]$"); from everything i've read it seems to work but it only works on certain numbers and says most other numbers are wrong I have attached my code. it seems to me that the expression is formed incorectly, beacue when i printed the result of regVal.test(document.f1.duration.value) to the screen it is actually deeming those numbers (for example 4) as not a match, so its not the if block or alert statement misfiring. if you could take a look at my code that would be very helpfull, its very short code.. and this is driving me bonkers...lol thanks in advance code: --------- function val2() { var regVal; regVal= new RegExp("^[1-366]$"); if (!regVal.test(document.f1.duration.value)) { alert('Please enter a duration between 1 and 366 days'); document.write (regVal.test(document.f1.duration.value)); //pls note the above line prints the result of the match to the screen . Every time the alert text says the match was false it really was false, not just a misfire of the alert text. } } |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You should read the forum FAQ/rules and one of the "How to Post..." threads. They clearly indicate that you should put you code in tags to preserve the indentation. Anything else is rude to your potential respondents.
That said, trying to validate that a number is within a certain range by using regular expressions is like trying to swat a spider with a track hoe. Simply compare it against the upper and lower limits after ascertaining that it is, indeed, a number, with one of the available JS tests. Your particular expression range, [1-366], says that you have a valid character set including the characters representing the numbers 1-3 (that would be 1,2, and 3), plus the character representing the number, 6. A number expressed as a string with any digits other than those will fail. JS is a weakly typed language. You still need to know, occassionally, the difference between a string representing a number, and an actual number.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
code in tags ...
function val2()
{
var regVal;
regVal= new RegExp("^[1-366]$");
if (!regVal.test(document.f1.duration.value))
{
alert('Please enter a duration between 1 and 366 days');
document.write (regVal.test(document.f1.duration.value));
//pls note the above line prints the result of the match to the screen . Every time the alert text says the match was false it really was false, not just a misfire of the alert text.
}
}
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Regular expressions are for pattern matching. If you need to learn and practice that, you picked the wrong "thing" to practice with.
function valNumber (n, minVal, maxVal)
{
if (!isNaN (n = parseInt (n)))
if ((n >= minVal) && (n <= maxVal)) return true; else return false;
}if (valNumber (c, 1, 366)) alert ("Passed"); else alert ("Failed");^[-+]?[0-9]+(\.[0-9]{1})?[0-9]*$
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|