Js verification Summary

Source: Internet
Author: User

The simplest method to verify if Javascript code is a number:

Use of the isNaN function:

Function checknum (){
If (isNaN (frm. num. value )){
Alert ("enter a number ");
Frm. num. focus ();

Return false;
}
}

The most complete and accurate method: (regular expression)

1)

"^ \ D + $" // non-negative 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 number + 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 floating point number + 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 number
2)

Var r =/^ \ +? [1-9] [0-9] * $/; // positive integer
R. test (str );

The most intuitive method:

<Script language = "javascript">
Function CheckMyForm (){
Var txt = myform. mytext. value;
If (checknumber (txt )){
Alert ("only numbers are allowed! ");
Return false;
}
Return true;
}

Function checknumber (String ){
Var Letters = "1234567890 ";
Var I;
Var c;
For (I = 0; I <Letters. length (); I ++) {// Letters. length ()->>>> length
C = Letters. charAt (I );
If (Letters. indexOf (c) =-1) {// "c" cannot be found in "Letters". For details, see the return value of this function.
Return true;
}
}
Return false;
}
</Script>

Expansion: ===================================================

CharAt

The charAt (int index) method is a String instance method that can be used to retrieve characters under a specific index.
The charAt () method returns a character at the provided parameter reference.
For example, str. chatAt (0) retrieves the first character in str, str. charAt (str. length ()-1) retrieves the last character.
The following example illustrates the usage 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]):
The usage is exactly the same as that of indexof.

The index of the first matching item of the specified character in this instance. Searches start from the specified character location and checks the specified number of character locations.

Parameters

Value

Unicode character to be searched. The search area of value is case sensitive.

StartIndex (Int32)

Optional. Start position of the search. If this parameter is not set, it starts from 0.

Count (Int32)

Optional. number of characters to be checked.

Return Value

If this character is found, it is the index position of the value; otherwise, it is-1.

IndexOf ()

Searches for the position of the specified character or string that appears for the first time in the string, and returns the first index value, for example:

Str1.IndexOf ("word"); // search for the index value (location) of the word in str1)

Str1.IndexOf ("string"); // search for the index value (location) of the first character of "string" in str1)

Str1.IndexOf ("Word", start, end); // query end characters starting from start + 1 in str1, search for the position of "word" in STR1 [starting from the first character] Note: start + end cannot exceed the length of str1

The indexof parameter is string. Search for the position where the parameter string appears for the first time in the string and return the position. For example, string s = "0123 dfdfdf"; int I = s. indexof ("df"); then I = 4.

If you need more powerful string parsing functions, use the Regex class and use regular expressions to match strings.

[Post] original URL: http://www.jb51.net/html/blog/1/23464.htm

Indexof (): locates the character and string from the front to the back in the string. All return values are absolute positions in the string. If it is null, it is-1.

String test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd ";

Test. indexof ('D') = 2 // locate the location where d appears for the first time.

Test. indexof ('D', 1) = 2 // locate the first position where d appears from the third string

Test. indexof ('D', 5, 2) = 6 // locate d from the front to the back and query it from 5th bits. Check for 2 bits, that is, from 5th bits to 7th bits;

Lastindexof (): locates the character and string from the back and forward in the string;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.