【问题标题】:jquery phone number validation with "match" error带有“匹配”错误的jquery电话号码验证
【发布时间】:2015-09-30 19:26:04
【问题描述】:

我正在使用 jquery 验证电话号码,格式如下:

+XX-XXXX-XXXX
+XX.XXXX.XXXX
+XX XXXX XXXX 

但是,它在控制台中给了我一个错误:

未捕获的类型错误:无法读取未定义的属性“匹配”

function phonenumber(inputtxt) {
	var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
	if(inputtxt.value.match(phoneno)) {
		return true;
		  }  
	else {  
		 alert("message");
		 return false;
		  }
		}
	jQuery(".btnchec").click(function(){
		var inputtxt = jQuery("#inputcpf").val();
		console.log(phonenumber(inputtxt));
		});
<fieldset>
	 <legend>Validation</legend>
	 <div style= "padding-top:15px;">
		 <label style= "padding: 20px; font-size: 16px; font-weight:600; color: #AA6903;">ID: </label>
		 <input type="text" id="inputcpf" name="cpf" size="18" maxlength="18" autofocus>
		  <input type="button" style= "margin-top:10px;" class="btnchec" name="Submit" value="Check">
		  </div>
</fieldset>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    inputtxt 是一个字符串,因此没有 .value 属性。

    变化:

    if(inputtxt.value.match(phoneno))
    

    收件人:

    if(inputtxt.match(phoneno))
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2016-05-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多