繁体   English   中英

如何验证电话字段只接受 8 位数字(不小于,不大于)

[英]how to validate for phone field to ONLY accept 8 digits (not less than, and not greater than)

我试图寻找答案,但是那里的代码版本太多了,像我这样的菜鸟很困惑……我需要电话字段只接受 8 位数字,否则表单将无法提交……

这是我得到的代码(我添加了 min 和 mac 长度,但没有用:

public function validate( $data )
{
$validator = new AB_Validator();
foreach ( $data as $field_name => $field_value ) {
switch ( $field_name ) {
case 'email':
$validator->validateEmail( $field_name, $data );
break;
case 'phone':
$validator->validatePhone( $field_name, $field_value, $min_length="8", $max_length="8", true );
break;
case 'date_from':
case 'time_from':
case 'time_to':
case 'appointment_datetime':
$validator->validateDateTime( $field_name, $field_value, true );
break;
case 'name':
$validator->validateString( $field_name, $field_value, 255, true, true, 3 );
break;
case 'service_id':
$validator->validateNumber( $field_name, $field_value );
break;
case 'custom_fields':
$validator->validateCustomFields( $field_value );
break;
default:
}
}

if ( isset( $data['time_from'] ) && isset( $data['time_to'] ) ) {
$validator->validateTimeGt( 'time_from', $data['time_from'], $data['time_to']             );
}

return $validator->getErrors();
}

我也尝试添加以下脚本,但它只给了我一个警告,我仍然可以单击提交...

<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var phone = document.getElementById('phoneno');

// Check each input in the order that it appears in the form!
        if(isNumeric(phone, "Please enter a valid phone number")){
            if(lengthRestriction(phone, 8, 8)){
                return true;

            }
        }

return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
    alert(helperMsg);
    elem.focus(); // set the focus to this input
    return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}



function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
    return true;
}else{
    alert("Please enter " +max+ " digits");
    elem.focus();
    return false;
}
}

</script>

<script>
$(document).ready(function(){
$("button.next").click(function(){
    $formValidator();
});
});
</script>

任何帮助将不胜感激...

“我也尝试添加以下脚本,但它只给了我一个警告,我仍然可以点击提交......”

如果代码有效,但您在收到一条警告,指出未满足提交条件后仍然可以单击提交(或者更重要的是,该表单是可提交的),则问题可能出在您的 <form> 元素中。 如果您的表单中有 onSubmit 事件处理程序(假设您这样做),则调用电话的验证代码。 不。 字段,您可以对 onSubmit 调用的函数进行编码以返回 false 或 true。 如果为假,则取消提交。 如果为真,则执行。

如果这对您来说都是新闻,请查看此 SO 问题以获取指导:

如何防止表单被提交?

在这和一些谷歌搜索之间,你应该找出你需要什么,假设这是你的问题。

在谷歌搜索和测试表单时,我注意到名称字段至少需要 3 个字符,所以我检查了代码并注意到了这一点:

case 'name':
$validator->validateString( $field_name, $field_value, 255, true, true, 3 );

所以我调整并将其添加到电话部分,该表格现在按预期工作,只接受 8 位数字。 这是添加的代码:

case 'phone':
$validator->validatePhone( $field_name, $field_value, true );
$validator->validateString( $field_name, $field_value, 8, true, true, 8 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM