jquery - Form submit callback returning multiple times? And how to create a function for a null value in another function? -
sorry double question, i'm encountering few issues try crack password change form. follow on previous question here submitting form if 'if' statements return true.
i have keyup function runs through if/else statements check password conforms number of requirements. example, if longer 7 characters, if contains capital letter, if contains 2 numbers etc. have created global variable 'rtntrue = 1' set 0 on else statements inside keyup function (if fail 1 of requirements rtntrue = 0). theory if rtntrue = 1, form can submitted, otherwise can't.
i have 2 problems:
- even variable global, still need set rule on submit action because rtntrue happen when user types in password input box. e.g. if press submit without typing anything, rtntrue = 1 anyway it'll submit. need along lines of pwd.length == 0 i'm not sure how because of variable being inside keyup function?
- if type incorrect in box , click submit, callback alert showing 3 times , don't know why!
here's js code , i've created jsfiddle here because it's quite long!
var rtntrue = 1; $('#password-info').hide(); $('#form-password-change #input-password').keyup(function() { // keyup code here // set password variable var pwd = $(this).val(); // validate length if (pwd.length > 7) { $('#length').removeclass('invalid').addclass('valid'); } else { $('#length').removeclass('valid').addclass('invalid'); rtntrue = 0; } // regexp // validate letter if ( /([^a-z]*[a-z]){3,}/i.test(pwd) ) { $('#letter').removeclass('invalid').addclass('valid'); } else { $('#letter').removeclass('valid').addclass('invalid'); rtntrue = 0; } // validate repeated letters (none repeated more twice) if ( /([a-za-z])(.*?\1){2}/.test(pwd) ) { $('#letter .repeated').removeclass('valid').addclass('invalid'); $('#letter').addclass('invalid-repeated'); } else { $('#letter .repeated').removeclass('invalid').addclass('valid'); $('#letter').removeclass('invalid-repeated'); rtntrue = 0; } // validate capital letter if (pwd.match(/[a-z]/)) { $('#capital').removeclass('invalid').addclass('valid'); } else { $('#capital').removeclass('valid').addclass('invalid'); rtntrue = 0; } // validate number if ( /([^\d]*[\d]){2,}/.test(pwd) ) { $('#number').removeclass('invalid').addclass('valid'); } else { $('#number').removeclass('valid').addclass('invalid'); rtntrue = 0; } // validate repeated numbers (none repeated more twice) if ( /([\d])(.*?\1){2}/.test(pwd) ) { $('#number .repeated').removeclass('valid').addclass('invalid'); $('#number').addclass('invalid-repeated'); } else { $('#number .repeated').removeclass('invalid').addclass('valid'); $('#number').removeclass('invalid-repeated'); rtntrue = 0; } passwordformsubmit(); // new function }).focus(function() { // focus code here $('#password-info').slidedown('fast'); }).blur(function() { // blur code here $('#password-info').show(); }); function passwordformsubmit() { var local = rtntrue; if (rtntrue==1) { $('#form-password-change').submit(function(){ return true; }); }else { $('#form-password-change').submit(function(){ alert(rtntrue); return false; }); } }
if want quick ways achieve goal, modified jsfiddle. try using true / false instead of 0 or 1. , trying submit form every time user types. see did in jsfiddle.
and try setting first variable false, if user click submit button no input, not submit.
var passwordfine = false;
Comments
Post a Comment