Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 申请表验证_Javascript_Html_Css - Fatal编程技术网

Javascript 申请表验证

Javascript 申请表验证,javascript,html,css,Javascript,Html,Css,嗨,伙计们,我正试图在申请表中做一些验证 我有3个文本框,用于不同的手机号码,如家庭、工作、手机等。。我需要3个字段中的1个字段。用户必须至少输入1个数字(共3个) 有什么方法可以做到这一点吗?您可以尝试下面给出的代码 <script type="text/javascript"> function checkValidation(){ var mobile = document.getElementById('mobile').value;

嗨,伙计们,我正试图在申请表中做一些验证

我有3个文本框,用于不同的手机号码,如家庭、工作、手机等。。我需要3个字段中的1个字段。用户必须至少输入1个数字(共3个)


有什么方法可以做到这一点吗?

您可以尝试下面给出的代码

 <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>

函数checkValidation(){
var mobile=document.getElementById('mobile').value;
var gome=document.getElementById('home').value;
var work=document.getElementById('work').value;
var错误计数=0;
如果(移动==''){
错误计数=错误计数+1;
} 
如果(主=“”){
错误计数=错误计数+1;
} 
如果(工时=“”){
错误计数=错误计数+1;
} 
如果(错误计数=3){
警报(“请从手机、工作、家庭联系人号码中至少添加一个!”;
返回false;
}否则{
返回true;
}
}
你可以试试这样的


谢谢

我希望这能帮助你找到你想要的东西。如果在提交时调用html表单验证()函数:

<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>

函数验证(){
var a=document.form.phone.value;
var b=document.form.mobile.value;
var c=document.form.home.value;
如果(a==”&&b==”&&c==”){
警报(“请至少输入一个数字”);
//如果需要,请在此处添加其他验证
document.form.phone.focus();//例如,聚焦第一个字段
返回false;
}
}

不要将输入值与“”或“”进行比较。用户只需键入空格即可。而是检查或修剪空白,并检查其是否为有效数字

<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>

//检查所有空格。
函数有空格{
返回/^\s+$/g.test;
}
//检查有效号码
函数isNumber(n){
return!isNaN(parseFloat(n))和&isFinite(n);
}
函数验证(){
var mobile=document.getElementById('mobile').value;
var home=document.getElementById('home')。值;
var work=document.getElementById('work').value;
var误差=0;
if(!(!haswitespace(mobile)和&isNumber(mobile))){
误差+=1;
} 
if(!(!haswitespace(home)和&isNumber(home))){
误差+=1;
} 
if(!(!haswitespace(work)和&isNumber(work))){
误差+=1;
} 
如果(错误==3){
警报(“您必须输入至少一个联系人号码”);
返回false;
}否则{
返回true;
}
}
在Submit上从表单调用validation()

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


希望这对jQuery有所帮助,以备不时之需

HTML:


主页
工作
移动电话
JS:

$(函数(){
$(“#form1”)。关于(“提交”,函数(){
var notempty=$(“.phones[value!=''”);//您可以扩展此测试

如果(notempty.length)请确认您知道如何验证一个,并且想要验证三个的条件,或者想要全部!@Dipali-请接受对您有用的解决方案!这将使其他人受益。谢谢!
<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>
​
$(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;
    });
}); ​