Font: [Increase decrease] Type: Reprint time: 2013-04-14 I want to comment
JS Verification is a summary of the numbers, the need for friends can refer to
JS verification is a number, the simplest way:
Use of the isNaN function:
function Checknum () {
if (IsNaN (Frm.num.value)) {
Alert ("Please enter a number");
Frm.num.focus ();
return false;
}
}
The most complete and accurate method: (Regular expression)
1)
"^\\d+$"//nonnegative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$"//Positive integer
"^ ((-\\d+) | (0+)) $ "//non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$"//Negative integer
"^-?\\d+$"//Integer
"^\\d+ ("//non-negative floating-point number (positive floating point + 0)
^ ([0-9]+\\]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $ "//positive floating-point number
"^ ((-\\d+ ("///non-positive floating-point number (negative float + 0)
^ (-([0-9]+\\]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $ "//negative floating-point number
^ (-?\\d+) ("//floating-point
2)
var r =/^\+? [1-9] [0-9]*$/; Positive integers
R.test (str);
The most intuitive method:
<script language= "JavaScript" >
function Checkmyform () {
var txt = myform.mytext.value;
if (CheckNumber (TXT)) {
Alert ("Only allow numbers! ");
return false;
}
return true;
}
function CheckNumber (String) {
var Letters = "1234567890";
var i;
var C;
for (i = 0; i < letters.length (); i + +) {//letters.length ()->>>> take character length
c = Letters.charat (i);
if (Letters.indexof (c) ==-1) {//Cannot find "C" in "Letters" see the return value of this function below
return true;
}
}
return false;
}
</script>
Outreach: =====================================
CharAt
The charAt (int index) method is a method that can be used to retrieve a string instance of a character under a particular fuse.
The CharAt () method returns a character that is located at the parameter anchor provided to it.
such as: Str.chatat (0) retrieves the first character in Str, Str.charat (Str.length ()-1) retrieves the last character.
The following example illustrates the use of the CharAt method:
<script language= "JavaScript" >
function Charattest (n) {
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//Initialize variable.
var s; Declare variable.
s = Str.charat (n-1); Get correct character//from position n–1.
return (s); Return character.
}
Alert (3);
</script>
IndexOf
String.IndexOf method (value, [StartIndex], [Count]):
Usage is exactly the same as indexof ().
Reports the index of the first occurrence of a specified character in this instance. The search starts at the specified character position and checks the specified number of character positions.
Parameters
Value
The Unicode character to find. The search for value is case-sensitive.
StartIndex (Int32)
Option to search for the starting position. No setting starts from 0.
Count (Int32)
Optional, the number of character positions to check.
return value
The index position of value if the character is found, or 1 if not found.
IndexOf ()
Finds the first occurrence of a specified character or string in a string, and returns the index value, such as:
Str1. IndexOf ("word");//Find the index value of "word" in str1 (position)
Str1. IndexOf ("string");//Find the index value (position) of the first character of the string in str1
Str1. IndexOf ("word", start,end);//from str1 start+1 characters, look for end characters, find the position of "word" in string STR1 [from the first character] Note: Start+end cannot be greater than str1 length
The indexof parameter is string, looking for the position of the first occurrence of the argument string in the string and returning the position. such as String s= "0123DFDFDF"; int i=s.indexof ("df"), then i==4.
If you need more powerful string parsing, you should use a Regex class to match strings with regular expressions.
[Repost] Original information url:http://www.jb51.net/html/blog/1/23464.htm
IndexOf (): Position characters and strings from the front to back in a string; all return values refer to the absolute position of the string, or 1 if null.
String test= "asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
Test.indexof (' d ') = 2//position from front to back for first occurrence of D
Test.indexof (' d ', 1) = 2//front-to-back position d from the first occurrence of a third string
Test.indexof (' d ', 5,2) = 6//front-to-back positioning D from 5th position, check 2, that is, from 5th to 7th;
LastIndexOf (): Positions characters and strings in a string from the back forward;
JS Validation is a summary of the numbers (RPM)