jQuery form validation on blur and on submit

Hello Experts,

I am writing a simple custom jQuery validation, example:


$(document).ready(function() {

var validEmail = new RegExp(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);

$('#contactEmail').blur(function() {
						
	if($(this).val().match(validEmail)) {

	    $(this).removeClass("error");			
	    } else {
	    $(this).addClass("error");
	}	
 });
});

Open in new window


I need to use the same for "on submit". Do i need to write the same code twice one for .blur and the other for .submit or is it possible to combine both?

Thank you for your help!
RefaelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/6jcZS/
$(document).ready(function() {

    var validEmail = new RegExp(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);

    var validateEmail = function(evt) {
        if($(this).val().match(validEmail)) {
            $(this).removeClass("error");
        } else {
            $(this).addClass("error");
            evt.preventDefault();
        }
    }

    $('#contactEmail').blur(validateEmail);
    $("#submitButtonID").click(function(evt) {
        validateEmail.call($('#contactEmail'),evt);
    });
});

Open in new window

0
RefaelAuthor Commented:
Hi leakim971 :-)

Always thank you,

If i have more validations i should simply add "evt" to their function and it will stop the form from submitting until all the conditions have met?
0
RefaelAuthor Commented:
Hi leakim971 :-)

I am trying that out. it seem to work perfect yet this function is having a problem.

var requiredInput = $("#contactform input[inputRequired]");

var validateRequired = function(evt) {
	
	$(requiredInput).each(function() {
					
		if (($.trim($(this).val() )=== "") || ($.trim($(this).val()).length <= 5)) {
							
			$(this).addClass("error");
			evt.preventDefault();
			
		} else {

			$(this).removeClass("error");
			$(this).next().text("");
		}
	});
}

$(requiredInput).blur(validateRequired);

$("#submit").click(function(evt) {

   validateRequired.call($(requiredInput),evt);

}

Open in new window

0
RefaelAuthor Commented:
Hi eakim971

Can you please help me resolve this. The only problem when applying your solution as in the example in the above comment is that all the required field are highlighted once i "blur" on any of the form fields. Thanks.
0
leakim971PluritechnicianCommented:
This select to much inputs : $("#contactform input[inputRequired]")
When the following select only one : $('#contactEmail')

If you need a full solution to validate your form don't reinvent the wheel, just use :
http://jqueryvalidation.org/
or :
http://demos.usejquery.com/ketchup-plugin/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.