Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 11th, 2005, 11:37 AM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Exclamation Regular Expression HELP -- thanks

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.


}

}
paulchwd is offline   Reply With Quote
Old Oct 11th, 2005, 12:45 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 11th, 2005, 1:13 PM   #3
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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! */
sykkn is offline   Reply With Quote
Old Oct 11th, 2005, 4:40 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
}
Invoke with something like:
if (valNumber (c, 1, 366)) alert ("Passed"); else alert ("Failed");
If you want to know if a series of characters will result in a valid number, define "valid". If "valid" is defined as an optional sign (+/-) in the first position, then at least one digit, then an optional decimal point followed by at least one digit, then an optional series of additional digits, you might use something like this:
^[-+]?[0-9]+(\.[0-9]{1})?[0-9]*$
Note that definition is YOUR business (or your client's/boss's). This one says a number can't begin or end with a bare decimal point. Someone else might like it defined otherwise. Note that it doesn't allow for scientific (exponential) notation. Some might want it otherwise. This little exercise is a waste of resources; the procedure given first is superior in that respect. Just because a regex produces one line of code does not mean it's efficient. There a ton of boocheet called into action under the hood.
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC