Jump to content

Recommended Posts

Hi I have written a custom script to validate a form that I am using, the problem I am having is that i have written the code so that classes are applied at particular stages of the validation depending on what the user is editing.

 

The code:

 

<script type="text/javascript">
$(function(){
var $submit = $("div.submitBtn input");
var $required = $(".required");
var $userLength = $("#usernameLength input").val().length;
function containsBlanks(){
var blanks = $required.map(function(){
return $(this).val() == "";
});
return $.inArray(true,blanks) != -1;
}

function isValidEmail(email){
return email.indexOf("@") != -1;
}

function requiredFields(){
if(containsBlanks()
|| $("#usernameLength input").val().length < 6
|| $("#usernameLength input").val().length > 30/* || !isValidEmail($("#email").val())*/)
$submit.attr("disabled","disabled");
else
$submit.removeAttr("disabled");
}

$("#registerOverlay span").hide();
$("#signIn").click(function(){
$("div.popOverlay").fadeIn("slow");
$("div#registerOverlay").fadeIn("slow");
});
$("div.popOverlay").click(function(){
$(this).fadeOut("slow");
$("div#registerOverlay").fadeOut("slow");
});
$("#registerOverlay input").focus(function(){
$(this).next().fadeIn("slow");
}).blur(function(){
$(this).next().fadeOut("slow");
}).keyup(function(){
// check all required fields
requiredFields();
});

$("#usernameLength input").keyup(function(){
//check string length of username
if($("#usernameLength input").val().length < 6)
$("#usernameLength span").removeClass("pass").addClass("error");
else if($("#usernameLength input").val().length > 30)
$("#usernameLength span").removeClass("pass").addClass("error");
else
$("#usernameLength span").removeClass("error").addClass("pass");
});

/*$("#email").keyup(function(){
// check for valid email
if(isValidEmail($(this).val()))
$(this).next().removeClass("error").addClass("pass");
else
$(this).next().removeClass("pass").addClass("error");
});*/

requiredFields();
});
</script>

 

The actual problem that I am having is that the addClass for the usernameLength span isnt working, well its applying the class, just not the styles, im not entirely sure why so I guessed it had something to do with this jQuery script. If you have any suggestions its much appreciated :-)

Link to comment
https://forums.phpfreaks.com/topic/273045-form-validation-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.