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

String Manipulation Question

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Guys,

This one is probably quite easy but I just don't know how to do it. This is basically what I want to do ( in rough pseudocode ).

A string and a character are passed to this js function:

function validNumber(string, firstChar) {

if (first char of string == firstChar) {
verify that from second char of string to end is a number
}
else {
verify that string is a number
}
}

I know that I can use isNan to verify whether or not a variable is numeric or not. It's just the fact that I have to check the first character. Can you please tell me if this can easily be done. Mise Le Meas,

Mighty :)
 
you could try,
Code:
function validNumber(stringObj, firstChar)
{
 if (stringObj.substr(0, 1) == firstChar)
 {
   code to run....
 }
 else
 {
  other code to run...
 }
}

Is this what you meant?
 
The command used to check a certain caracter in a string is :
Yourvar.charAt(0)
The first caracter is 0 the last caracter is:
Yourvar.length - 1
You can check the following script below:

<script>
function checknr() {
nrvar = document.test.nrstr.value
i1 = 0;
while(i1 < nrvar.length) {
//According to isNaN the space is allso a number
if(isNaN(nrvar.charAt(i1)) || nrvar.charAt(i1) == &quot; &quot;) {
alert(&quot;Please enter a number value&quot;)
break;
}
i1++
}
}
</script>

<form name = &quot;test&quot;>
<input type = &quot;text&quot; name = &quot;nrstr&quot;><br>
<input type=&quot;button&quot; name=&quot;but&quot; value=&quot;klick here&quot; onclick=&quot;checknr()&quot;><br>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top