Home >Web Front-end >JS Tutorial >jquery form validation cannot be numeric

jquery form validation cannot be numeric

WBOY
WBOYOriginal
2021-11-16 09:55:412204browse

Methods for jquery form verification that is not numeric: 1. Use the val method to obtain the form value; 2. Create a regular expression to verify numbers "/^[0-9] $/"; 3. In if Use the test method in the statement to verify whether the form value matches the regular expression. The syntax is "if (regular expression object. test (form value))".

jquery form validation cannot be numeric

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

How jquery form validation cannot be numeric

1. Create a new html file named test.html to explain jquery validation The form cannot be written as pure numbers. In the test.html file, use the form tag to create a form. Within the form form, use the input tag to create an input box for the form.

In the test.html file, in the form form, use the button tag to create a button with the button name "Verify Member Name".

In the test.html file, bind the onclick event to the button button. When the button is clicked, the yanzheng() function is executed.

jquery form validation cannot be numeric

2. In the js tag, create the yanzheng() function. Within the function, obtain the input object through the id (user), and use the val() method to obtain its input Member name, save the member name in the user variable.

In the yanzheng() function, create a regular expression "/^[0-9] $/" to verify numbers. In the if statement, verify whether the member name matches the regular expression through the test() method. formula, if it matches, it will prompt "cannot be a pure number".

jquery form validation cannot be numeric

3. Open the test.html file in the browser, enter the member name, click the button, and check the effect.

jquery form validation cannot be numeric

The regular expression "/^[0-9] $/" represents one or more numbers starting with a number and ending with a number.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of jquery form validation cannot be numeric. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn