views:

115

answers:

1

This is a form validation that will hide the submit button if the input field(s) are not valid, and show it if all the input fields are valid.

Why is the submit button kept invisible in firefox after all inputs have been made valid once again, when that doesn't happen in other browsers?

This is the javascript in question:

$(document).ready(function(){
$("input[type='text'],input[type='password']").focus(function(){
    $(this).siblings('.hideit').removeClass('hideit').parent('div').addClass('phover').siblings('div').removeClass('phover').children('p,strong:not(.invalid,.valid,.hidev)').addClass('hideit');
});
$("input[type='text'],input[type='password']").blur(function(){
    $(this).siblings('p,strong:not(.invalid,.valid,.hidev)').addClass('hideit').parent('div').removeClass('phover');
});
$("input[type='text'],input[type='password']").change(function(){
    var theval = $(this).val();
    var theexp = $(this).attr("reg");
    var ivalid = new RegExp(theexp);
    if (theval.search(ivalid) == -1) {
        $(this).siblings('strong.hidev,strong.valid').removeClass('hidev valid').addClass('invalid').text("x");
        showsubmit($(this));
        } else {
            $(this).siblings('strong.hidev,strong.invalid').removeClass('hidev invalid').addClass('valid').text("=");
            showsubmit($(this));
    }

});
function showsubmit(x){
    $("input[type='text'],input[type='password']").each(function(){
            var getval = $(this).val();
            var getreg = $(this).attr("reg");
            var itest = new RegExp(getreg);
            if (getval.search(itest) == -1) {
                $("p.sub").fadeOut(1000);
                return false;
             } else {
            $("p.sub").fadeIn(1000);
            return true;
            }
    });
}

});

<div id="formb">
    <form name="tform1" id="form1">
        <div ><label for="names">NAMES:</label><strong class="prepend-2 hideit">Your names (Last, First and/or middle).</strong><br /><input type="text" name="name" id="names" reg="^([a-zA-Z]{2,},)((\s[a-zA-Z]{2,})|(\s[a-zA-Z]{2,}){2})$" /><strong class="hidev"></strong><p class="prepend-2 hideit hintf">example: BILL, GATES JOHN</p></div>
        <div><label for="usern">USERNAME:</label><strong class="prepend-4 hideit">Choose a username.</strong><br /><input type="text" name="user" id="usern" reg="^[a-zA-Z]{6,}$" /><strong class="hidev"></strong><p class="prepend-1 hideit hintf">min 6, without special character or numbers</p></div>

        <div><label for="userp">PASSWORD:</label><strong class="prepend-4 span-3 hideit">Choose a password.</strong><br /><input type="password" name="pass" id="userp" reg="^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)(?=.*[#@!&]+.*)[0-9a-zA-Z#@!&]{6,}$" /><strong class="hidev"></strong><p class="hintf hideit">min. 6, must contain alphabet(s), number(s) & xter(#, @, &, !)</p></div>
        <div><label for="email">E-MAIL ADDRESS:</label><strong class="prepend-2 hideit">Input your email address.</strong><br /><input type="text" name="mail" id="email" reg="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$" /><strong class="hidev"></strong><p class="prepend-2 hideit hintf">E-mail address must be valid for usage.</p></div>

            <p class="sub">
                <input type="submit" value="Submit" />

            </p>

    </form>
</div>
+4  A: 

I think the answer may lie in the fact that you are showing and hiding the submit button as you evaluate each input. Instead, I hid the submit button right away and changed "showsubmit" to look like this.

function showsubmit(x){
var hideIt = 0;
$("input[type='text'],input[type='password']").each(function(){
    var getval = $(this).val();
    var getreg = $(this).attr("reg");
    var itest = new RegExp(getreg);
    hideIt |= (getval.search(itest) == -1);
});
if (hideIt) {
    if ($("p.sub").is(":visible")) {
      $("p.sub").fadeOut(1000);
    }
    return false;
} else {
    $("p.sub").fadeIn(1000);
    return true;
}
}

The sample is available on JS Bin http://jsbin.com/eyexa4/3/edit

jedatu
thanks boo! tested and working fine...
joberror
hi jedatu! I tested this on the JS BIN website and it works perfectly but didn't work locally, what might be wrong. even I copied the generated JS BIN HTML code to my local files still same on firefox..
joberror
Is the version of jQuery on the JS Bin example the same as in your project?
jedatu
yes! but it is not working locally
joberror
please can u try this locally in firefox..
joberror
I am guessing your local page has all your other CSS and JS, so it's highly possible that something else on the page is causing it to break. I would need access to all that code as well in order to check the CSS, check the JS error console and/or step through the JS using Firebug.
jedatu