Click to See Complete Forum and Search --> : regex


mili
10-16-2003, 02:13 PM
Hi,

I have a problem with a regex for decimal validation that behaves very wierdly.
/[0-9]+\.[0-9]+/

If there is a 0 in the number, it works
Eg: 11.210

Otherwise it doesnt
Eg: 11.210

Pls help

Thanks

Jona
10-16-2003, 02:30 PM
<form action="" name="myForm" onsubmit="val(this.textObj.value); return false;"><div>
<input type="text" name="textObj"><br>
<input type="submit" value="Validate">
</div></form>
<script type="text/javascript"><!--
function val(obj){
var re = /^[0-9]+\.[0-9]+$/;
if(re.exec(obj)){alert("valid");}
else {alert("invalid");}
}
//--></script>


[J]ona

mili
10-16-2003, 03:37 PM
Thanks Jona..
What is the significance of ^ & $?

Jeff Mott
10-16-2003, 03:40 PM
If there is a 0 in the number, it works
Eg: 11.210

Otherwise it doesnt
Eg: 11.210This is supposed to be an example that works and an example that doesn't, right? Why are they the same?

mili
10-16-2003, 04:05 PM
Sorry, it is:

If there is a 0 in the number, it works
Eg: 11.210

Otherwise it doesnt
Eg: 11.211

Jeff Mott
10-16-2003, 07:31 PM
When I tested, these both returned true.alert(/[0-9]+\.[0-9]+/.test('11.210'));
alert(/[0-9]+\.[0-9]+/.test('11.211'));

Jona
10-16-2003, 08:07 PM
Originally posted by mili
Thanks Jona..
What is the significance of ^ & $?

^ means it "starts with" and $ means it "ends with." Together, "it starts with and ends with" the RegEx in between them.

A good link for reference is: http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html

[J]ona