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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression - Decimal values 2

Status
Not open for further replies.

stewartwebb

Programmer
Jan 6, 2004
92
GB
I have several text fields, all are decimal numbers. Each decimal field is of different format e.g can be 123.45, 1234.56, 12345.67 etc.
Is there a way that I can pass the value of the field and the format (4,2 for example) and do a regular expression to check that the value matches the format?

Thanks for any help
 
Code:
function testDec(sVal,nDig,nDec) {
  var sRe = "^\d{" + nDig + "}\.\d{" + nDec + "}$";
  var re = new RegExp(sRe);
  if ( re.test(sVal) )
    // valid value
  else
    // invalid value
}

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
a small change to tracy's script:

var sRe = "^\d{1," + nDig + "}\.\d{1," + nDec + "}$";

set a range rather than fixed digits...


Known is handfull, Unknown is worldfull
 
Seems to me that your change pretty much defeats the purpose of what he wants to do. It looked like to me he wants to REQUIRE a SPECIFIC number of digits before and after the decimal. Otherwise he wouldn't need a function where he could pass the number of digits and decimals, just a generic function.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
>> Each decimal field is of different format e.g can be 123.45, 1234.56, 12345.67

the maximum and minimum is 4 and 2...

Known is handfull, Unknown is worldfull
 
I've tried the above examples and this is what I now have -

Code:
var DecimalValueCorrect;
function check1DecimalValue(inputName,integer,decimal) 
  {
  var inputValue = inputName.value;
  var sRe = "^\d{1," + integer + "}\.\d{1," + decimal + "}$";
  alert(sRe + " " + inputValue)
  var re = new RegExp(sRe);
  if (re.test(inputValue))
    {
    DecimalValueCorrect = "Y";
	}
  else
    {
    DecimalValueCorrect = "N";
	}
}

Unfortunately this doesn't work. The only value it works for is when I enter 1 number.
Sorry if I didn't explain my problem properly in the first place but I do need a range. The maximum for example for the input field could be 7.2 but the user could enter a 4.2 or even a .2 and both of these are valid values.
Anyone have any ideas?

Cheers

Stewart.
 
<script>
var DecimalValueCorrect;
function check1DecimalValue(inputName,integer,decimal)
{
var inputValue = inputName;
var sRe = "^\\d{1," + integer + "}\.\\d{1," + decimal + "}$";
var re = new RegExp(sRe);
if (re.test(inputValue))
{
return "Y";
}
else
{
return "N";
}
}

alert(check1DecimalValue("5.23","5","2"))
</script>

Known is handfull, Unknown is worldfull
 
Thanks vbkris but that still didn't work. It always returns N. Any other ideas?
 
Don't worry it something else that was the problem, cheers vbkris
 
One more bug:

<script>
var DecimalValueCorrect;
function check1DecimalValue(inputName,integer,decimal)
{
var inputValue = inputName;
var sRe = "^\\d{1," + integer + "}\\.\\d{1," + decimal + "}$";
var re = new RegExp(sRe);
if (re.test(inputValue))
{
return "Y";
}
else
{
return "N";
}
}

alert(check1DecimalValue("15123.0","5","2"))
</script>

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top