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!

How to search a string

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
How would I search a string for a . (decimal point)

All I want is something like:

If decimal point exists{
DO THIS
}
else{
DO THIS
}

Thanks,
Mark
 
Code:
if(stringVar.indexOf(".") > -1)
 do this
else
 do that
 

Try this out.

<html>
<head>
<script language= &quot;JavaScript&quot;>
function ckdec() {
var refstring = /\./g; //sets ref for char to be searched for
var mystrg = document.form1.testval.value;
if (mystrg.search(refstring) > -1) { // if search returns found value > -1
alert (&quot;you have a decimal point!&quot;); }
else { alert (&quot;you did not enter a decimal point!&quot;);}
}
</script>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;testval&quot; size=&quot;15&quot; value=&quot;enter a num&quot;>
<input type=&quot;button&quot; value=&quot;click to test&quot; onClick=&quot;ckdec();&quot;>
</form>
</body
</html>

Good Luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top