3

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>  
Raghul Rajendran
  • 486
  • 2
  • 7
  • 25
  • works fine **[here](https://jsfiddle.net/Guruprasad_Rao/jfnuvd8u/)** You try wrapping your code in `head` and if possible try to keep it on `keypress` for key press and hold event **[DEMO](https://jsfiddle.net/Guruprasad_Rao/jfnuvd8u/1/)** – Guruprasad J Rao Aug 10 '15 at 09:15
  • it working in chrome but not working in mozilla. – Raghul Rajendran Aug 10 '15 at 09:30

2 Answers2

1

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;
}
Community
  • 1
  • 1
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
1

Try to use jquery, it will work in each browser

function validate() {  
console.log("validation start");
    var msg;  
    if($("input[name='userPass']").val().length >5)
        console.log("good");//msg="good";  
    else
       console.log("poor");// msg="poor";  
    document.getElementById("mylocation").innerText=msg;  
}
Farhan
  • 1,453
  • 2
  • 15
  • 20