faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

How do I confirm that the first 2 (or other) characters in a string are zeros.

Jan 12th, 2004 15:06
Jean-Bernard Valentaten, Elson Browne,


Using the methods of the String object, this task is accomplished (and 
programmed) in no time:
var myString = '';
//do something with myString
...
//check whether the first two chars are zeros
if (myString.substr(0, 2) = '00')
{
  //do something
  ...
}
//check whether the two characters starting at n-th position are zeros
if (myString.substr(n-1, 2) = '00')
{
}
//do something else
...
If you just want to check the position of the two zeros use the method 
indexOf() (which will return -1 if '00' is not found):
var myString;
//do something with myString
...
//check whether '00' is contained in myString
if (myString.indexOf('00') != -1)
{
  //do something
  ...
}
HTH,
Jean