Home > Web Front-end > JS Tutorial > body text

JS implements digital and email checking functions before submitting the form

韦小宝
Release: 2018-01-08 10:51:29
Original
1248 people have browsed it

In project development, we often encounter the form submission function. Today I take the time to share with you JS to implement the number and email verification function before submitting the form. What are my thoughts on js For those who are interested, please refer to JS to implement the function of checking the numbers and email before submitting the form.

I personally feel that when a project allows users to submit a form, the data type submitted by the user and content are highly uncertain, and users may not fully understand the designer’s meaning. Therefore, proofreading before submitting the form is necessary. Here are a few proofreading methods. I am a newbie, so please give me some advice.


<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.replace函数 这里用来去掉空格等于trim
    return false;d
  }else{
    if(isNaN(age)||age<0){       //如果age是特殊的非数字值NaN(或者能被转换为这样的值),返回的值就是true。如果age是其他值,则返回false。
      alert("请输入正确的年龄(数字)")
      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]+$/; //匹配邮箱
      isok= reg.test(email);
    if(!isok){
      alert("邮箱格式不正确,请重新输入!");
      document.getElementById("email").focus();
      return false;
    }
  }
}
 return true;
</script>
Copy after login



<form action="${pageContext.request.contextPath }/updateinfo" onsubmit="return toVaild()" >
<center>
<input type="text" id="age" placeholder="年龄">
<input id="email" placeholder="email" >
<button type="submit" >保存修改</button> </center>
</form>
Copy after login


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

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

replace(match/* string ORregular expression */, replacement/* string OR function*/)

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

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

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

The second parameter can be a string, which can contain the grouping of the regular expression of the first parameter, for example:

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

Summary

The above is the JS introduced by the editor to implement the number and email verification function before submitting the form. I hope it will be helpful to you. Everyone helps! !

Related recommendations:

javascript matches the regular expression code commented in js

JS implementation of simple array deduplication method example

js verification phone number and mobile phone support +86 regular expression

The above is the detailed content of JS implements digital and email checking functions before submitting the form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template