tags:

views:

69

answers:

2

I have added some javascript in html page for input validation.same page is working correct in IE and chrome but in mozila its not working.The problem is when user inputs invalid data its supposed to show alert msg box and when user clicks OK it should return false to form...BUT mozila is not waiting for alert box it just shows alert box for 5-6 sec and then goes to next page defined in form action="nextpage.php"

 function validate_form(thisform) 
    {
    with (thisform) 
        {
        if (validate_required(oldpassword, "<b>Error: </b>Please enter the Old Password!") == false)
        { changeColor("oldpassword"); return false; }

        else if (valid_length(newpassword, "<b>Error: </b>Please enter the New Password!!") == false)
        {newpassword.value=""; changeColor("newpassword"); return false; }

        else if (valid_length(cnfpassword, "<b>Error: </b>Please enter the Confirm Password!!") == false)
        {cnfpassword.value=""; changeColor("cnfpassword"); return false; }

        else if (document.getElementById('newpassword').value != document.getElementById('cnfpassword').value)
        {changeColor("newpassword");cool.error("<b>Error: </b>Passwords entered are not same!"); 
        newpassword.value="";cnfpassword.value="";return false;}

    }
}function validate_required(field, alerttxt) 
    {
    with (field) 
        {
        if (value == null || value == "") 
            {
           cool.error(alerttxt);return false;
            }
        else 
            {
            return true;
            }
        }
    }

cool.error is nothing but CSS nd Js for alert box.I thing there is not any problem in my code weather problem is in some browser settings.Is it so??? because it is working fine in IE and Chrome.

+4  A: 

You're using a non-standard IE-only behavior that creates global variables for every element with an ID.

To fix it, add a global variable for each element that you use:

var oldpassword = document.getElementById("oldpassword");

Also, you should never use Javascript's with block.
It is very different from VB's with block, and should not be used unless you truly understand its pitfalls and shortcomings. It will also slow down your code.

SLaks
sorry I didn't get you...can u explain more...
nectar
Add lines like `var oldpassword = document.getElementById("oldpassword");` outside of your functions for each HTML Element that you use in Javascript.
SLaks
Or `myformobject.elements.oldpassword` if you're using names only. But using IDs is easier.
bobince
Like this???--- function validate_form(thisform) { var oldpassword = document.getElementById("oldpassword"); var newpassword = document.getElementById("newpassword"); var cnfpassword = document.getElementById("cnfpassword"); if (validate_required(oldpassword, "<b>Error: </b>Please enter the Old Password!") == false) { changeColor("oldpassword"); return false; } else if (valid_length(newpassword, "<b>Error: </b>Please enter the New Password!!") == false) {newpassword.value=""; changeColor("newpassword"); return false; }
nectar
how you submit code line in comments.what shortcut you use?
nectar
this is not working, even I have removed **with** still its not working...???
nectar
What error do you get? What is `ChangeColor`?
SLaks
Please edit your question to include the additional code.
SLaks
A: 

You could also use some javascript library to get past browser issues. I'm rooting for jQuery.

JHollanti
can jquery be used for validation as javascript?
nectar