javascript中的验证不能用于多个if else语句?

时间:2015-05-18 07:42:58

标签: javascript

下面是我的javascript代码 - 只要点击代理单选按钮,它就会进入第一个验证部分而不会检查其他部分:

function validate(form1) 
    {
        var agent1=document.getElementById('agent');
        var confirm=document.getElementById('confirm');
        var holding=document.getElementById('holding');
        var confirm=form1.confirm.value;
        var book_by=form1.bookby.value;
        var bkother=form1.bookbyother.value;
        var agntphn=form1.phone1.value;
        var gstnam=form1.guest_name.value;
        var gstphn=form1.guest_no.value;
        var adlts=form1.no_adults.value;
        var children=form1.no_child.value;
        var infants=form1.no_infants.value;
        var check_in=form1.checkin.value;
        var check_out=form1.checkout.value;
        var nfd1=form1.no_of_days.value;
        var rc=form1.roomtype.value;
        var rpd=form1.rp_day.value;
        var rpb=form1.extrarate.value;
        var tamt=form1.totalamount.value;
        var advamt=form1.advance_amount.value;

        if ((agent1.checked == true))
        {
            if(!(book_by || bkother))
            {
                alert('Choose the valid agent')
            }
            else if(agntphn =="")
            {
                alert('Enter the agent phone number')
            }
        }
        else if (gstnam=="") {
            alert("Enter a valid Name ");
            document.getElementById('guest_name').focus();
            return false;
            //errors[errors.length] = "Enter valid Name ";
        }
        else if (!ck_Mobileno.test(gstphn)) {
            alert("Enter a valid Mobile Number with 10 digits");
            document.getElementById('guest_no').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if ((adlts=="") &&( children=="" )&&( infants=="")) {
            alert("Enter a valid No of persons");
            document.getElementById('no_adults').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(check_in)) {
            alert("Enter a valid Checkin Date");
            document.getElementById('checkin').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(check_out)) {
            alert("Enter a valid  check_out date");
            document.getElementById('checkout').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(nfd1)) {
            alert("Click to the box to get the no of days");
            document.getElementById('no_of_days').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(rc)) {
            alert("Select the room type");
            document.getElementById('roomtype').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(rpd)) {
            alert("Enter the Rate Per Day");
            document.getElementById('rp_day').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(rpb)) {
            alert("Enter the Rate Per Bed");
            document.getElementById('extrarate').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(tamt)) {
            alert("Click on the box to get the total amount");
            document.getElementById('totalamount').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        else if (!(advamt)) {
            alert("Enter the advance amount");
            document.getElementById('advance_amount').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }

    }

</script>

http://services.odata.org/OData/OData.svc/DemoService.Persons

点击确认单选按钮后,所有禁用元素都会显示出来......

2 个答案:

答案 0 :(得分:1)

假设您的表单正在执行

<form onsubmit="return validate(this)"
  1. 修理其他部分。大多数其他地方都没有必要,特别是代码中的第一个将在检查代理时停止其余的验证。我添加了一些代码来处理选择或键入的代理的验证 - 当它变得那么大时,单独的函数会更好
  2. 更改||和&amp;&amp;此处&GT; if(!(book_by && bkother)) {
  3. 重命名并更改表单末尾第二个bookby字段的ID
  4. 从phone1中删除空格或在测试之前使用trim删除空格
  5.     if (agent1.checked) {
          if (book_by == "") {
            alert('Choose a valid agent');
            document.getElementById('bookby').focus();            
            return false;
          }
          else {
            if (bkother == "") {
              alert('Type an agent name');        
              document.getElementById('bookbyother').focus();            
              return false;
            }
            if (agntphn == "") {
              alert('Enter the agent phone number');
              document.getElementById('phone1').focus();           
              return false;
            }
          }
        }
        if (gstnam=="") {
            alert("Enter a valid Name ");
            document.getElementById('guest_name').focus();
            return false;
            //errors[errors.length] = "Enter valid Name ";
        }
        if (!ck_Mobileno.test(gstphn)) {
            alert("Enter a valid Mobile Number with 10 digits");
            document.getElementById('guest_no').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if ((adlts=="") &&( children=="" )&&( infants=="")) {
            alert("Enter a valid No of persons");
            document.getElementById('no_adults').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(check_in)) {
            alert("Enter a valid Checkin Date");
            document.getElementById('checkin').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(check_out)) {
            alert("Enter a valid  check_out date");
            document.getElementById('checkout').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(nfd1)) {
            alert("Click to the box to get the no of days");
            document.getElementById('no_of_days').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(rc)) {
            alert("Select the room type");
            document.getElementById('roomtype').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(rpd)) {
            alert("Enter the Rate Per Day");
            document.getElementById('rp_day').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(rpb)) {
            alert("Enter the Rate Per Bed");
            document.getElementById('extrarate').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(tamt)) {
            alert("Click on the box to get the total amount");
            document.getElementById('totalamount').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
        if (!(advamt)) {
            alert("Enter the advance amount");
            document.getElementById('advance_amount').focus();
            return false;
            //errors[errors.length] = "Enter valid mobileno ";
        }
      return true; // allow submission
    }
    

答案 1 :(得分:0)

您正在使用if (condition) {} else if (condition) {}。匹配的第一个条件是将要执行的所有条件。

如果您希望执行所有测试,请删除else

    if ((agent1.checked == true))
    {
        if(!(book_by || bkother))
        {
            alert('Choose the valid agent');
            return false;
        }
        else if(agntphn =="")
        {
            alert('Enter the agent phone number')
            return false;
        }
    }

    // == > don't use else if here, just a plain if
    if (gstnam=="") {
        alert("Enter a valid Name ");
        document.getElementById('guest_name').focus();
        return false;
    }

    // ... continuing on for the rest of your if statements

如果基于数据,您只想要执行某些测试,那么您必须将代码分解为块并使用正确的if测试来确定哪些代码执行哪些条件。但是,你拥有它的方式只会执行匹配的第一个条件和其他条件的 none