如何提醒给定的手机号码是否有效?

时间:2017-02-14 10:44:37

标签: javascript jquery

J查询代码是:

    allNextBtn.click(function(){

    //TODO: more validation if needs

    if($("#mobile").val() == ''){
        $("#mobile").focus();
        $("#mobile_valid").addClass('has-error');
        return;
    }

    var curStep = $(this).closest(".setup-content"),
        curStepBtn = curStep.attr("id"),
        nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
        curInputs = curStep.find("input[type='text'],input[type='url']"),
        isValid = true;

    $(".form-group").removeClass("has-error");
    for(var i=0; i<curInputs.length; i++){
        if (!curInputs[i].validity.valid){
            isValid = false;
            $(curInputs[i]).closest(".form-group").addClass("has-error");
        }
    }

    if (isValid)
        nextStepWizard.removeAttr('disabled').trigger('click');
});

这是我验证手机号码的代码。在这里,我需要提醒输入的手机号码是否有效以及如何在上述代码中进行操作?

1 个答案:

答案 0 :(得分:3)

请尝试以下代码:

function validateMobNo(mobno){
    var mobno2;
    var flag=false;
    var mlen= mobno.length;
    //alert(mobno.substr(3,mobno.length-3));
    if(mobno.charAt(0)!='+' && mlen==10){
        mobno2="+91-"+mobno;
        alert("1>>your mobile wants to be in "+mobno2);
        flag=true;
    }
    else if(mobno.charAt(0)=='+'){
        if(mobno.substr(0,3)=='+91' && mobno.length==13){
            mobno2=mobno.substr(0,3)+"-"+mobno.substr(3,mobno.length-3);
            alert("2>>your mobile wants to be in "+mobno2);
            flag=true;      
        }
    }
    else if(mobno.indexOf("-")<0&&mobno.length==12 && mobno.substr(0,2)=='91'){
        mobno2=mobno.substr(0,2)+"-"+mobno.substr(3,mobno.length-2);
        alert("3>>your mobile wants to be in "+mobno2);
        flag=true;
        }
    else
        alert("Please correct your mobile No");
    if(flag==true)
       document.mobvalidate.mobno.value=mobno2;
    else
        document.mobvalidate.mobno.focus()
    return flag;
         
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="mobvalidate">
<input type="text" id="mobno" />
<input type="button" value="VALIDATE" onclick="validateMobNo(mobno.value)" />
</form>