Skip to main content
deleted 49 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Would like help on improving my jQuery Validationvalidation code

Would like some assistance in improving my jQuery validation code. I have some repeated code, but I am not sure how to write it in a way so that I don't have to keep repeating the same code over and over again for different variable names etc... (such as the ifif statement part) My goal is to make it so that I can easily apply it to any other form I create.

Here is the jsFiddle

Javascript:

Thanks for your help in advance!

Would like help on improving my jQuery Validation code

Would like some assistance in improving my jQuery validation code. I have some repeated code, but I am not sure how to write it in a way so that I don't have to keep repeating the same code over and over again for different variable names etc... (such as the if statement part) My goal is to make it so that I can easily apply it to any other form I create.

Here is the jsFiddle

Javascript:

Thanks for your help in advance!

jQuery validation code

Would like some assistance in improving my jQuery validation code. I have some repeated code, but I am not sure how to write it in a way so that I don't have to keep repeating the same code over and over again for different variable names etc... (such as the if statement part) My goal is to make it so that I can easily apply it to any other form I create.

Here is the jsFiddle

Rollback to Revision 1
Source Link
SirPython
  • 13.5k
  • 3
  • 38
  • 93

UPDATE CODE: (SAME FUNCTIONALITY)

$('form.requiredFields').submit(function(e) { var req = $(this).find('.req'), validateEmail = function(email) { var re = /^(([^<>()[\]\\.,;:\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,}))$/; return re.test(email); }; req.each(function() { var $this = $(this), defaultVal = $this.prop('defaultValue'); if ( ( $this.hasClass('email') && !validateEmail( $this.val() ) ) || (defaultVal === $this.val() || $this.val() === '' || $this.val().length < 3) ) { $this.addClass('_error') } else { $this.removeClass('_error req'); } }); console.log(req.length); if ( req.length === 0 ) { return true; } else { return false; } }); 

UPDATE CODE: (SAME FUNCTIONALITY)

$('form.requiredFields').submit(function(e) { var req = $(this).find('.req'), validateEmail = function(email) { var re = /^(([^<>()[\]\\.,;:\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,}))$/; return re.test(email); }; req.each(function() { var $this = $(this), defaultVal = $this.prop('defaultValue'); if ( ( $this.hasClass('email') && !validateEmail( $this.val() ) ) || (defaultVal === $this.val() || $this.val() === '' || $this.val().length < 3) ) { $this.addClass('_error') } else { $this.removeClass('_error req'); } }); console.log(req.length); if ( req.length === 0 ) { return true; } else { return false; } }); 
Question Protected by Ethan Bierlein
added 845 characters in body
Source Link

UPDATE CODE: (SAME FUNCTIONALITY)

$('form.requiredFields').submit(function(e) { var req = $(this).find('.req'), validateEmail = function(email) { var re = /^(([^<>()[\]\\.,;:\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,}))$/; return re.test(email); }; req.each(function() { var $this = $(this), defaultVal = $this.prop('defaultValue'); if ( ( $this.hasClass('email') && !validateEmail( $this.val() ) ) || (defaultVal === $this.val() || $this.val() === '' || $this.val().length < 3) ) { $this.addClass('_error') } else { $this.removeClass('_error req'); } }); console.log(req.length); if ( req.length === 0 ) { return true; } else { return false; } }); 

UPDATE CODE: (SAME FUNCTIONALITY)

$('form.requiredFields').submit(function(e) { var req = $(this).find('.req'), validateEmail = function(email) { var re = /^(([^<>()[\]\\.,;:\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,}))$/; return re.test(email); }; req.each(function() { var $this = $(this), defaultVal = $this.prop('defaultValue'); if ( ( $this.hasClass('email') && !validateEmail( $this.val() ) ) || (defaultVal === $this.val() || $this.val() === '' || $this.val().length < 3) ) { $this.addClass('_error') } else { $this.removeClass('_error req'); } }); console.log(req.length); if ( req.length === 0 ) { return true; } else { return false; } }); 
Source Link
Loading