2012-11-02 101 views
-2

嗨,大家好,我正在嘗試在申請表中做一些驗證。申請表驗證

我對如家,工作不同的手機號碼,3文本框中,移動這樣的..我需要3.用戶的1個強制必須有輸入數字的3

至少1是否有任何如何做到這一點?

+2

是的,有.. – Jocelyn

+0

請清除您知道如何驗證一個,並希望條件三的驗證或想整個事情! – MJQ

+0

@Dipali - 請接受對您有用的解決方案!它會使其他人受益。謝謝! – Usha

回答

1

等給出如下

<script type="text/javascript"> 
     function checkValidation(){ 
      var mobile = document.getElementById('mobile').value; 
      var gome = document.getElementById('home').value; 
      var work = document.getElementById('work').value; 
      var error_count = 0; 

      if(mobile == ''){ 
       error_count = error_count + 1; 
      } 
      if(home == ''){ 
       error_count = error_count + 1; 
      } 
      if(work == ''){ 
       error_count = error_count + 1; 
      } 

      if(error_count == 3){ 
       alert("Please add at least one from mobile ,work,home contact no.! "); 
       return false; 
      }else{ 
        return true; 
      } 
     } 
</script> 

你可以嘗試這樣的事情你可以試試代碼。

謝謝

1

我希望這會幫助你找到你想要的東西。如果你的HTML表單上提交了調用validation()函數:

<script type="text/javascript"> 
function validation() { 
    var a = document.form.phone.value; 
    var b = document.form.mobile.value; 
    var c = document.form.home.value; 
    if(a=="" && b =="" && c =="") { 
    alert("Please Enter at least one Number"); 
    //add other validation if you need here 
    document.form.phone.focus(); // focus the first field for example 
    return false; 
    } 
} 
</script> 
1

不要把輸入值與「」或「'比較。用戶可以簡單地鍵入空格。請檢查或修剪空白並檢查它是否是有效的數字。

<script type="text/javascript"> 

//Check for all whitespace. 
function hasWhiteSpace(s){ 
    return /^\s+$/g.test(s); 
} 

//Check for valid number 
function isNumber(n) { 
     return !isNaN(parseFloat(n)) && isFinite(n); 
} 

function validation(){ 
    var mobile = document.getElementById('mobile').value; 
    var home = document.getElementById('home').value; 
    var work = document.getElementById('work').value; 

    var error = 0; 

    if(!(!hasWhiteSpace(mobile) && isNumber(mobile))){ 
     error += 1; 
    } 
    if(!(!hasWhiteSpace(home) && isNumber(home))){ 
     error += 1;  
    } 
    if(!(!hasWhiteSpace(work) && isNumber(work))){ 
     error += 1; 
    } 

    if(error == 3){ 
     alert("You must enter at least one contact number."); 
     return false; 
    }else{ 
     return true; 
    } 
} 
</script> 

從窗體onsubmit調用驗證()。

<form onsubmit="return validation();"> 

JavaScript Number Validation

JavaScript Check For White Space

希望這有助於

0

jQuery的情況下,你希望它

DEMO

HTML:

<form id="form1"> 
    Home <input type="phone" class="phones"><br/> 
    Work <input type="phone" class="phones"><br/> 
    Mobile <input type="phone" class="phones"><br/> 
    <input type="submit"> 
</form> 
​ 

JS:

$(function(){ 
    $("#form1").on("submit",function() { 
     var notempty = $(".phones[value!='']"); // you can extend this test 
     if (notempty.length<1) { 
      alert('Please fill at least one number'); 
      $(".phones:first").focus(); 
      return false; 
     } 
     return true; 
    }); 
}); ​ 
+0

謝謝。它的用處。 – Dipali