Home  >  Article  >  Web Front-end  >  JS implements digital and email checking functions

JS implements digital and email checking functions

小云云
小云云Original
2017-12-21 13:28:371594browse

When users are asked to submit a form in a project, the data type and content submitted by the user are highly uncertain, and the user may not fully understand the designer's meaning. Therefore, proofreading before submitting the form is necessary. This article will share with you the JS implementation of digital and email proofreading functions. I hope it can help everyone.

<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>


<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>

The JavaScript String.replace function is to replace the match in the string with replacement and return 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 OR regular expression*/, replacement/* string OR function*/)

Function Replaces 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.

Related recommendations:

Let’s talk about javascript dynamically add style rules W3C proofreading_javascript skills

Sent when mobile phone registration Verification code countdown function

PHP verification login user name and password

The above is the detailed content of JS implements digital and email checking functions. 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