Search code examples
javascriptjquerykeyupjquery-form-validator

In Java Script validation Using keyup for validating is not working in mozilla


In jquery form validation the keyup is not working mozilla but working fine in chrome.

here is the Javascript code

function validate() {  
    var msg;  
    if(document.myForm.userPass.value.length>5)
        msg="good";  
    else
        msg="poor";  
    document.getElementById("mylocation").innerText=msg;  
}  

here is the html code

<form name="myForm"> 
<input type="password" value="" name="userPass" onkeyup="validate()">   
Strength:<span id="mylocation">no strength</span>  
</form>  

Solution

  • From this answer it tells that Firefox doesn't take innerText or innerHTML to set a value, rather it takes textContent to set a value. So use textContent to set your value if the browser is Firefox

    DEMO

    function validate() {  
        var msg;  
        if(document.myForm.userPass.value.length>5){  
            msg="good";  
        }  
        else{  
            msg="poor";  
        }  
        var f=navigator.userAgent.search("Firefox"); //check if browser if FF
        if(f>-1)
            document.getElementById("mylocation").textContent=msg //if yes use this
        else
            document.getElementById("mylocation").innerText=msg //else normal approach
    }  
    

    Note :- As I suggested in comment use onkeypress instead of onkeyup for key press and hold events validation.

    DEMO

    To identify different browsers you can use below code from this answer

    function checkBrowser(){
        c=navigator.userAgent.search("Chrome");
        f=navigator.userAgent.search("Firefox");
        m8=navigator.userAgent.search("MSIE 8.0");
        m9=navigator.userAgent.search("MSIE 9.0");
        if (c>-1){
            brwsr = "Chrome";
        }
        else if(f>-1){
            brwsr = "Firefox";
        }else if (m9>-1){
            brwsr ="MSIE 9.0";
        }else if (m8>-1){
            brwsr ="MSIE 8.0";
        }
        return brwsr;
    }