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

String Validation 1

Status
Not open for further replies.

UncleHuckleberry

Technical User
Dec 4, 2003
58
GB
I'm new to javascript and i'm trying to create a validation function that will check if the string input is correct. The string should only be numeric values seperated by commas e.g. 12345,23456,34567

can someone please suggest the best way to approach this.

Thanks.

"When the going gets wierd, the wierd turn pro" - R. Duke
 
hi
you can use regexp,
or you can scan the string character by character (a `for' loop) and check if it is digit or comma.

here is the regexp, solution:
Code:
<SCRIPT>
function is_valid_input(str)
{
    return !(/[^\d,]/.test(str.toString()));
}

alert("12,34" + ' '+ is_valid_input("12,34"));
alert(123 + ' '+is_valid_input(123));
alert("12 34" + ' '+is_valid_input("12 34"));
</SCRIPT>

PM

___
____
 

Do you want to allow whole numbers only? What about negative numbers?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks predamarcel, thats perfect.

"When the going gets wierd, the wierd turn pro" - R. Duke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top