JS verification is the sum of the numbers _javascript skills

Source: Internet
Author: User

JS verification is a number, the easiest 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+$"//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+ ()//nonnegative 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 way:

<script language= "JavaScript" >
function Checkmyform () {
var txt = myform.mytext.value;
if (CheckNumber (TXT)) {
Alert ("Only allow numbers to be entered!") ");
return false;
}
return true;
}

function CheckNumber (String) {
var letters = "1234567890";
var i;
var C;
for (i = 0; i < letters.length (); i + +) {//letters.length ()->>>> character length
c = Letters.charat (i);
if (Letters.indexof (c) ==-1) {//No "C" found in "letters" see the following return value for this function
return true;
}
}
return false;
}
</script>

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

CharAt

The charAt (int index) method is a method that can be used to retrieve a string instance of a character under a particular anchor.
The CharAt () method returns a character located at the parameter anchor provided to it.
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 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]):
The usage is exactly the same as indexof ().

Reports the index of the first occurrence of the 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. Searches for value are case-sensitive.

StartIndex (Int32)

Option to search the starting position. is not set to start at 0.

Count (Int32)

Optional, the number of character positions to check.

return value

If the character is found, the index position of value, or 1 if not found.

IndexOf ()

Finds the first occurrence of a specified character or string in a string, and returns the first index value, such as:

Str1. IndexOf ("word");//Find the index value (position) of the word in str1

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, find the end character, find the position of "word" in the string STR1 [from the first character] Note: Start+end cannot be greater than the length of str1

The indexof parameter is string, looking for the first occurrence of the parameter string in the string and returning the position. such as String s= "0123DFDFDF"; int i=s.indexof ("df"); then i==4.

If you need a more powerful string parsing feature, you should use the Regex class to match the string with a regular expression.

[Paste] Original information url:http://www.jb51.net/html/blog/1/23464.htm

IndexOf (): The character and string are positioned backwards in the string; all return values are in the absolute position of the string, such as null-1

String test= "asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

Test.indexof (' d ') = 2//front and back position D first occurrence

Test.indexof (' d ', 1) = 2//front and back position d the first occurrence of a third string

Test.indexof (' d ', 5,2) = 6//front-backward positioning D from the 5th position, check 2 digits, that is, from 5th to 7th;

LastIndexOf (): Locates characters and strings from behind in a string;

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.