Remove all commas and validate to a usable float/integer

Remove all commas and validate to a usable float/integer

Since Javascript doesn't like to add on commas (wink), get rid of them and then validate the field to make sure it contains all usable positive numbers.

If a non-usable number is found, it redirects you back to the field, selects the fields contents for you and turns the field background to yellow. When it is corrected, the field background will turn back to white (onBlur). These colors are, of course, changeable.

Enjoy, -Mike


Step 1: Paste this into your JS header:

function IsInt (string)
	{if(string == 0) 
	  { 
	  var val = parseFloat (string);
	  return val > -1
	  }
	 else 
	  {
	  var val1 = string / 1;
	  var val = parseFloat(val1);
	  return val > -1
	  }	
	}

Step 2: Paste this into your fields onBlur event:

var f = document.forms[0];

//remove all commas in a number
newString = this.value.replace(/,/g , "" ); 
this.value = newString;

//Test for usable value
if( !IsInt(this.value)) {alert( "This field contains a value that is negative 
or is not an integer or decimal." ) ; 
this.focus() ; this.style.backgroundColor='yellow'} 
if( IsInt(this.value))  {this.style.backgroundColor='white'} ;
	

Step 3: Paste the following into the onFocus field event:

select()

    Requires Free Membership to View

    Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

This was first published in December 2001

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.