<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> website effect number and fax number validation function </title>
<script>
function Checkifnumber (str)
{
var letters = "1234567890";
var i;
var C;
for (i = 0; i < str.length i + +)
{
c = Str.charat (i);
if (Letters.indexof (c) < 0)
Alert (' Not all numbers ');
}
Alert (' all numbers ');
}
function Checkiffax (str)
{
var letters = "1234567890-";
var i;
var C;
for (i = 0; i < str.length i + +)
{
c = Str.charat (i);
if (Letters.indexof (c) < 0)
Alert (' Not facsimile number ');
}
Alert (' facsimile number ');
}
</script>
<body>
<form id= "Form1" Name= "Form1" method= "Post" action= "" >
<label for= "TextField" ></label>
<input type= "text" name= "V" id= "TextField"/>
<input type= "button" name= "button" id= "button" value= "number" onclick= "Checkifnumber (form1.v.value);"/>
<input type= "button" Name= "Button2" id= "button2" value= "Fax" onclick= "Checkiffax (form1.v.value);" />
</form>
</body>
Use the function description.
The Charat () method returns the character at the specified position.
Note that JavaScript does not have a character data type that is different from a string type, so the character returned is a string of length 1.
Grammar
Stringobject.charat (Index) parameter description
Index required. A number that represents a position in a string, that is, the subscript of a character in a string.
The indexof () method returns the location of the first occurrence of a specified string value in a string.
Grammar
Stringobject.indexof (Searchvalue,fromindex)