Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Validation 2

Status
Not open for further replies.

KOVMoe

Programmer
Jun 30, 2004
34
US
I need to limit the number input into a field to less than 1,000,000. The user might enter the infroamtion as 99999, or 22,222, and so on.

I have tried this:

var objChk2 = /^(\d{0,2}\,\d{0,3})$/;
but that will not allow numbers less than 10,000

then i tried
var objChk5 = /^(\d\d\,\d\d\d)$/;
but it will not allow any thing less than 1000

Any ideas would be GREAT!


Thank you,

[king]Moe-King of the Village Idiots.

"When in trouble,
when in doubt;
Run in circles-
SCREAM & SHOUT!!"
Burma Shave
 
>I need to limit the number input into a field to less than 1,000,000. The user might enter the infroamtion as 99999, or 22,222, and so on.
But looking at what your attempts, I'm not sure whether you mean 100,000 or 1,000,000. This is how.

[1] Less than 100,000 - thousand separator (,) allowed
[tt] var rx=/^([1-9][0-9],?\d{3}|[1-9],?\d{3}|[1-9\d{2}|[1-9]\d{1}|[0-9])$/;[/tt]

[2] Less than 1,000,000 - thousand separator (,) allowed
[tt] var rxx=/^([1-9][0-9][0-9],?\d{3}|[1-9][0-9],?\d{3}|[1-9],?\d{3}|[1-9\d{2}|[1-9]\d{1}|[0-9])$/;[/tt]
 
ok, both are great, and work. This is what I came up with:
function validateForm() {
frm = document.frmDataInputs;
var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
//number from not to exceed 999,999
var objChk6 = /^(\d{1,3},\d{3}|\d{1,3})$/;
//number not to exceed 99,999
var objChk5 = /^(\d{1,2},\d{3}|\d{1,3})$/;
//number not to exceed 9,999
var objChk4 = /^(\d{1,1},\d{3}|\d{1,3})$/;
//number not to exceed 999
var objChk3 = /^(\d{3}|\d{1,3})$/;
//number not to exceed 999, with decimals, 2 digits.
var objChD2 = /^(\d{3}|\d{1,3})|(d{1,3}\.\d{1,2})$/;
var strErrorMessage = "";
var blnIsValid = true;

Thank you,

[king]Moe-King of the Village Idiots.

"When in trouble,
when in doubt;
Run in circles-
SCREAM & SHOUT!!"
Burma Shave
 
lol WTG cory

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
hey hey... i don't want your charity.

i was just trying to understand why a regex solution was needed, when indeed they're more of a resource hog than some non-regex solutions.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
it wasn't charity, is was a well earned star!

lol - you're looking for logic when there isn't any

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
parseInt:

easy? yes
resource-hog? no
sexy? [!]hell no![/!]

There you go cory - you have your answer.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
[laughtears] roflmfao -> there goes that sexy kaht!

[rofl]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
ahh, interesting. from w3schools:

Note: Only the first number in the string is returned!

Note: Leading and trailing spaces are allowed.

Note: If the first character cannot be converted to a number, parseInt() returns NaN.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
then i propose a simpler reg ex:

Code:
    var num = "1,000,000";
    var re = /\D+/g;
    if (parseInt(num.replace(re, "")) > 500000) {
        alert("can't be greater than 500 g's");
    }



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
silly cory [smile]

Code:
    var num = "400,000[!].123[/!]";
    var re = /\D+/g;
    if (parseInt(num.replace(re, "")) > 500000) {
        alert("can't be greater than 500 g's");
    }

Gotta make sure you don't kill the periods (and the + is not necessary if you use the global parameter):
Code:
var re = /[^0-9.]/g;

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
>much easier than the parseInt solution...
But the parseInt solution as such does not deliver, mind you.
 
...which I intended to salvage in the 2nd post, otherwise, I wouldn't even bother to post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top