JS implements the digit and email verification function before submitting the form, js form

Source: Internet
Author: User

JS implements the digit and email verification function before submitting the form, js form

I personally think that when a user submits a form in a project, the data type and content submitted by the user are of great uncertainty, and the user may not fully understand the meaning of the designer. Therefore, the school inspection before submitting the form is required. Here are a few school inspection methods to share with you. For more information, please advise.

<Script type = "text/javascript"> function toVaild () {var age = document. getElementById ("age "). value; var email = document. getElementById ("email "). value; if (age = null | age. replace (/(^ \ s *) | (\ s * $)/g, "") = "" | age = undefined) {// JavaScript String. the replace function is used to remove spaces that are equal to trim return false; d} else {if (isNaN (age) | age <0) {// If age is a special non-numeric value NaN (or can be converted to such a value), the returned value is true. If age is another value, false is returned. Alert ("enter the correct age (number)") document. getElementById ("age "). focus (); return false ;}}if (email = null | email. replace (/(^ \ s *) | (\ s * $)/g, "") = "" | email = undefined) {} else {var reg =/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $/; // match email isok = reg. test (email); if (! Isok) {alert ("the email format is incorrect. Please enter it again! "); Document. getElementById (" email "). focus (); return false ;}} return true; </script>
<Form action = "$ {pageContext. request. contextPath}/updateinfo "onsubmit =" return toVaild () "> <center> <input type =" text "id =" age "placeholder =" age "> <input id =" email "placeholder =" email "> <button type = "submit"> Save changes </button> </center> </form>

The JavaScript String. replace function replaces match in the source String with replacement and returns the replaced String. The usage is described as follows:

The function signature of the replace function of js String is as follows:

Replace (match/* string OR regular expression */, replacement/* string OR function */)

The function is to replace match in the source string with replacement and return the replaced string.

Therefore, the first parameter is usually a regular expression, for example:

Replace (/n/g, "h") // replace all n from the string with h.

The second parameter can be a string that contains the grouping of the first parameter regular expression. For example:

Replace (/(a) {2, 2}/g, "$ 1b") // replace all aa in the source string with AB.

Summary

The above section describes how to implement the digit and email verification function before submitting the form in Javascript. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.