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

Update my Regular Expression to allow negatives

Status
Not open for further replies.

checkai

Programmer
Joined
Jan 17, 2003
Messages
1,629
Location
US
here's what I have that does numbers great, I just need to allow for a negative sign as the first input...

Code:
function checkcurrency(thefield,s) {
  var iscur = /^\d+(\.\d\d)?$/.test(thefield.value);
  if(iscur) {
    thefield.style.backgroundColor = cok;
    return true;
  } else {
    thefield.style.backgroundColor = cbad;
    if (s != '') { 
      alert(s);
    }
    thefield.focus();
    return false;
  }  
}

"...your mom goes to college..."
 
If I could give myself a star, I would...found the answer...

Code:
var iscur = /(-)?\d+(\.\d\d)?$/.test(thefield.value);

"...your mom goes to college..."
 
on a side note you can get rid of the parentheses around the dash, and use the instance operator instead of 2 instances of \d - useful when you'd need to check for large instances of the same character:
Code:
var iscur = /-?\d+(\.\d[!]{2}[/!])?$/.test(thefield.value);

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top