开发者

Adding function is disabling other functions in the js file

开发者 https://www.devze.com 2023-01-30 08:00 出处:网络
I had this function in a js file and everything was working just fine : function check_acco_form() { var name=$(\"#name\").val();

I had this function in a js file and everything was working just fine :

function check_acco_form()
{
    var name=$("#name").val();
       var institution=$("#institution").val();
       var year=$("#year").val();

       //PNR Data
    var pnr1=$("#pnr1").val();
    var pnr2=$("#pnr2").val();
    // Arrival date info
    var arr_year=$("#arr_year").val();
    var arr_month=$("#arr_month").val();
    var arr_date=$("#arr_date").val();
    //Departure date info
    var dep_year=$("#dep_year").val();
    var dep_month=$("#dep_month").val();
    var dep_date=$("#dep_date").val();

    var numericExpression = /^[0-9]+$/;

       //Name, institution and year must not be empty
       if(name=="" || institution=="" || year=="")
    {
        alert("One or more fields are empty.");
              return;
       }
    //PNR must be all numbers
    if(!pnr1.match(numericExpression) || !pnr2.match(numericExpression))
    {   
        alert("A PNR number consists of 10 digits only. Please enter again.");
        $("#pnr1").val("");
        $("#pnr2").val("");
        return;
    }
    if(pnr1.length!=3 || pnr2.length!=7)
    {
        alert('Invalid PNR Number.');
        $("#pnr1").val("");
        $("#pnr2").val("");
        return;
    }

    if((arr_month==dep_month && dep_date<arr_date) || (dep_month<arr_month))
    {
        alert('Invalid dates.Please check again.');
        return;
    }

    //Test passed. Store in database;
    URL="saveAcco.php";
    parameters="name="+name+"&ins="+institution+"&year="+year+"&pnr="+pnr1+""+pnr2+"&dateArr="+arr_year+"-"+arr_month+"-"+arr_date+"&dateDep="+dep_year+"-"+dep_month+"-"+dep_date;
    $.get(URL+"?"+parameters,function(data){
        $("#msg_box").html(data);
        if(data=="Your changes have been saved." || data=="Your data has been saved and is pending approval.")
              {
            $("#acco_status").html('<br/><b>Accomodation Approval Status</b> : <span style="padding:3px;background-color:#f4fb3c">Approval Pending</span><br/><br/>');
              }
        $("#msg_box").fadeIn("slow",function(){
            setTimeout('fadeOutMsgBox();',3000);
              });
          });
}

I made a little changes to this function (added the variables 'mobile_num' and 'train_num', included 'if' conditions to make sure the user enters numbers only and made changes to the jQuery get function) that which resulted in the following code:

function check_acco_form()
{
    //Personal Information
    var name=$("#name").val();
    var institution=$("#institution").val();
    var year=$("#year").val();

    //Contact Information
    var mobile_num=$("#mobile").val();

       //PNR Data
    var pnr1=$("#pnr1").val();
    var pnr2=$("#pnr2").val();

    //Train Number
    var train_num=$("#trainnum").val();

    // Arrival date info
    var arr_year=$("#arr_year").val();
    var arr_month=$("#arr_month").val();
    var arr_date=$("#arr_date").va开发者_开发知识库l();
    //Departure date info
    var dep_year=$("#dep_year").val();
    var dep_month=$("#dep_month").val();
    var dep_date=$("#dep_date").val();

    var numericExpression = /^[0-9]+$/;

    //Name, institution and year must not be empty.
    if(name=="" || institution=="" || year=="")
    {
        alert("One or more fields are empty.");
        return;
    }

    //PNR can be empty but if entered must be all numbers
    if(pnr1!="" and pnr2!="")
    {
        if(!pnr1.match(numericExpression) || !pnr2.match(numericExpression))
        {   
            alert("A PNR number consists of 10 digits only. Please enter again.");
            $("#pnr1").val("");
            $("#pnr2").val("");
            return;
        }

        if(pnr1.length!=3 || pnr2.length!=7)
        {
            alert('Invalid PNR Number.');
            $("#pnr1").val("");
            $("#pnr2").val("");
            return;
        }
    }

    //Train number can be empty but if entered must be all numbers
    if(train_num!="")
    {
        if(!train_num.match(numericExpression))
        {   
            alert("Train number must consits of digits only");
            $("#trainnum").val("");
            return;
        }
    }

    //Mobile num can be empty but must be all numbers
    if(mobile_num!="")
    {
        if(!mobile_num.match(numericExpression))
        {
            alert("Invalid mobile number");
            $("#mobile_num").val("");
            return;
        }
        if(mobile_num.length!=10)
        {
            alert('A mobile number consists of 10 digits.Please enter again.');
            return;
        }
    }

    if((arr_month==dep_month && dep_date<arr_date) || (dep_month<arr_month))
    {
        alert('Departure date cannot be before arrival date.Please check again.');
        return;
    }

    //Test passed. Store in database;
    URL="saveAcco.php";
    parameters="name="+name+"&ins="+institution+"&year="+year+"&pnr="+pnr1+""+pnr2+"&dateArr="+arr_year+"-"+arr_month+"-"+arr_date+"&dateDep="+dep_year+"-"+dep_month+"-"+dep_date+"&mobile="+mobile_num+"&train_num="+train_num;
    $.get(URL+"?"+parameters,function(data){
        $("#msg_box").html(data);
        if(data=="Your changes have been saved." || data=="Your data has been saved and is pending approval.")
              {
            $("#acco_status").html('<br/><b>Accomodation Approval Status</b> : <span style="padding:3px;background-color:#f4fb3c">Approval Pending</span><br/><br/>');
            $("#acco_letter_print").html('Download accomodation letter <a href="PDF/acco_print.php" target="_blank">here</a>');
            $("#acco_letter_print").fadeIn();           
              }
        $("#msg_box").fadeIn("slow",function(){
            setTimeout('fadeOutMsgBox();',3000);
              });
          }); //End of get function

}

After the changes, suddenly all functions in the js file of this function have stopped working including this function. On searching the forum , I found this discussion : JavaScript function causing all other functions not to work inside js file which says that the error may be due to use of reserved words. However , I can't find any reserved words used as variables in my code. Any ideas what may the problem?


You have this in there:

if(pnr1!="" and pnr2!="")

It should be:

if(pnr1!="" && pnr2!="")

Any syntactical errors like this will cause the entire thing to fail, be sure to check your error console for things like this, they'll quickly point out the cause.


As an aside, try not to pass a string to setTimeout() as well, pass the function reference directly, changing this:

setTimeout('fadeOutMsgBox();',3000);

To this:

setTimeout(fadeOutMsgBox,3000);

This will give less issues, and allow the function to be anywhere in scope, it doesn't have to be global (like it would with a string).

0

精彩评论

暂无评论...
验证码 换一张
取 消