2017-02-14 47 views
0

歼查询代码为:如何警告给定的手机号码是否有效?

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'); 
}); 

这是我的代码验证手机号码。这里我需要给警报输入手机号码是否有效与否,我怎么能使其在上面的代码?

回答

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>