zoukankan      html  css  js  c++  java
  • (转)js 正则表达式之test函数讲解

    该方法的返回值是布尔值,通过该值可以匹配字符串中是否存在于正则表达式相匹配的结果,如果有匹配内容,返回ture,如果没有匹配内容返回false,该方法常用于判断用户输入数据的合法性,比如检验Email的合法性
    功能介绍:该方法的返回值是布尔值,通过该值可以匹配字符串中是否存在于正则表达式相匹配的结果,如果有匹配内容,返回ture,如果没有匹配内容返回false,该方法常用于判断用户输入数据的合法性,比如检验Email的合法性

    基本语法:objReg.test(objStr)
    objReg 必选项 RegExp对象名称
    objStr 要进行匹配检测的字符串
    讲解实例代码:

    复制代码 代码如下:
    <html>
    <script language="javascript" type="text/javascript">
    /*Designed By Androidyue*/
    /*
    功能:检测Email地址的合法性
    */
    function checkEmail(){
    //获取文本框中用户输入Email的信息
    var objStr=document.getElementById("email").value;
    //设置匹配Email的正则表达式
    var objReg=//w+[@]{1}/w+[.]/w+/;
    //document.write(objStr);
    //如果判断字符串中是否存在匹配内容,如果存在提示正确信息,否则返回错误
    if(objReg.test(objStr)){
    alert("该Email地址是合法的!");
    }else{
    alert("该Email地址是非法的!");
    }
    }
    </script>
    <body>
    请输入Email地址:
    <input type="text" id="email"><!--设置Email输入框-->
    <input type="button" value="检测合法性" onclick="checkEmail()"><!--设置按钮用于出发检测Email合法性事件-->
    </body>
    </html>

    详细出处参考:http://www.jb51.net/article/31562.htm

  • 相关阅读:
    bottle support gb2312
    solr 1.4.0 multi core deploy
    view file encoding on ubuntu
    bottle support gb2312
    multicore solr deploy process(not complete)
    SOLR Performance Benchmarks – Single vs. Multicore Index Shards
    ubuntu查看进程占用端口命令
    seo
    ubuntu不能更新包
    bdb虽好,不要忘了monetDB哦
  • 原文地址:https://www.cnblogs.com/wanshutao/p/3691668.html
Copyright © 2011-2022 走看看