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!

Validate Currency values 1

Status
Not open for further replies.

nissan240zx

Programmer
Joined
Jun 26, 2005
Messages
280
Location
US
Hello All,
As part of my ongoing validation issues..here is a new one.

Some of my fields in the form are currency fields and need to add some validations around it to make it "dummy proof".

Here is the scenario

Code:
<tr>
<td>Annual Revenue, if known ($):</td>
<td><input type=text style="width: 285px;" class="textbox" name="business_rev" size=20 maxlength="12" value="" onChange="document.frm.business_rev.value=commaSplit(document.frm.business_rev.value);">
</td>
</tr>

For example I want make this field to accept a maximum of 12 numbers (which I believe is fixed by using maxlength=12).
Now on top of that I want to make sure that ofcourse no more that 12 digits are entered and the values are formatted with a script to make the value look like
000,000,000,000
This I accomplished with this code
Code:
<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function commaSplit(srcNumber) {
var txtNumber = '' + srcNumber;
if (isNaN(txtNumber) || txtNumber == "") {
alert("That does not appear to be a valid number.  Please try again.");
fieldName.select();
fieldName.focus();


}
else {
var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
var arrNumber = txtNumber.split('.');
arrNumber[0] += '.';
do {
arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
} while (rxSplit.test(arrNumber[0]));
if (arrNumber.length > 1) {
return arrNumber.join('');
}
else {
return arrNumber[0].split('.')[0];
      }
   }
}
//  End -->
</script>

Now the final question is...I want to add a condition that "perdiods" should be allowed but should not be counted in the allowed 12 digits...

For example if I enter 1111111111.25
The form should accept that as 12 values and not 13.

HELP
Please advice...

Nissan

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
You will need to dynamically set the maxlength attribute based upon whether you detect a '.' in the value or not. You'll probably need to do this in the onKeyDown event... something like this:

Code:
onkeydown="checkMaxLength(this);

with this script:

Code:
function checkMaxLength(formEl) {
   if (formEl.value.indexOf('.') != -1) {
      formEl.maxLength = '12';
   } else {
      formEl.maxLength = '13';
   }
}

I've not tested this, but it should be fine.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan..this works super...
Genius....

A good programmer is someone who looks both ways before crossing a one-way street. - Doug Linder
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top