This seems like a problem I just cant fix, I'm working on a form that needs to validate the field on change, If a field isn't valid I want the wrap to display red and add a red border around the inupt field so the user is aware what section of the form is currently invalid and what input isn't correct.
If anyone can help me I'd truly appreciate it. I've attached my js,html and screen shots, I'm working on woocommerce and wordpress. If anyone can help me fix this i'll be more than happy to help return the favour.
JS
$('form.cart')
/* start ! This section adds & removes red backgroud and border */
.on( 'blur change', '.input-text', function() {
$(".single-attendee-wrapper .input-text").each(function(){
var $this = $(this);
var validated = true;
if ( $(this) ) {
if ( $this.val() == '' ) {
$(".single-attendee-wrapper .count").css('background','#ED616A');
$(".single-attendee-wrapper .count").css('color','#fff');
validated = false;
}
}
if ( validated ) {
$(".single-attendee-wrapper .count").css('background','#D1D3D4');
$(".single-attendee-wrapper .count").css('color','#808285');
}
});
} )
/* This section adds & removes red backgroud and border ! Finish */
.on( 'blur change', '.input-text, select', function() {
var $this = $(this);
var $parent = $this.closest('.form-row');
var validated = true;
if ( $parent.is( '.validate-required' ) ) {
if ( $this.val() == '' ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
}
}
if ( $parent.is( '.validate-email' ) ) {
if ( $this.val() ) {
/* http://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
var pattern = new RegExp(/^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((")(((( | )*(
))?( | )+)?(([--]|!|[#-[]|[]-~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([-
-]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*((( | )*(
))?( | )+)?(")))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i);
if ( ! pattern.test( $this.val() ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' );
validated = false;
}
}
}
if ( validated ) {
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field' ).addClass( 'woocommerce-validated' );
}
} )
HTML
<form enctype="multipart/form-data" method="post" class="cart" action="/hardy-group/product/manual-handling-3/?add-to-cart=414" style="background: none repeat scroll 0% 0% transparent;">
<h3 class="addon-name">One Attendee </h3>
<p class="form-row form-row-wide addon-wrap-414-one-attendee woocommerce-validated validate-required">
<label>Candidate Name </label> <input type="text" value="" name="addon-414-one-attendee-candidate-name" data-price="" class="input-text addon addon-custom" style="border-top: 0px none;">
</p>
<p class="form-row form-row-wide addon-wrap-414-one-attendee woocommerce-validated validate-required">
<label>Address </label> <input type="text" value="" name="addon-414-one-attendee-address" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-one-attendee validate-required woocommerce-validated">
<label>Candidate Email </label> <input type="text" value="" name="addon-414-one-attendee-candidate-email" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-one-attendee validate-required woocommerce-invalid woocommerce-invalid-required-field">
<label>Phone </label> <input type="text" value="" name="addon-414-one-attendee-phone" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-one-attendee validate-required woocommerce-invalid woocommerce-invalid-required-field">
<label>N.I Number </label> <input type="text" value="" name="addon-414-one-attendee-n-i-number" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-one-attendee validate-required woocommerce-invalid woocommerce-invalid-required-field">
<label>EUSR Number </label> <input type="text" value="" name="addon-414-one-attendee-eusr-number" data-price="" class="input-text addon addon-custom">
</p>
<div class="clear"></div>
</div>
<div class="count item1" style="background: none repeat scroll 0% 0% rgb(237, 97, 106); color: rgb(255, 255, 255);">1</div></div><div class="single-attendee-wrapper item2"><div class=" product-addon product-addon-two-attendees" style="display: block;">
<h3 class="addon-name">Two Attendees </h3>
<p class="form-row form-row-wide addon-wrap-414-two-attendees validate-required woocommerce-validated">
<label>Candidate Name </label> <input type="text" value="" name="addon-414-two-attendees-candidate-name" data-price="" class="input-text addon addon-custom" style="border-top: 0px none;">
</p>
<p class="form-row form-row-wide addon-wrap-414-two-attendees validate-required woocommerce-validated">
<label>Address </label> <input type="text" value="" name="addon-414-two-attendees-address" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-two-attendees woocommerce-validated validate-required">
<label>Candidate Email </label> <input type="text" value="" name="addon-414-two-attendees-candidate-email" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-two-attendees validate-required woocommerce-validated">
<label>Phone </label> <input type="text" value="" name="addon-414-two-attendees-phone" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-two-attendees woocommerce-validated validate-required">
<label>N.I Number </label> <input type="text" value="" name="addon-414-two-attendees-n-i-number" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-two-attendees woocommerce-validated validate-required">
<label>EUSR Number </label> <input type="text" value="" name="addon-414-two-attendees-eusr-number" data-price="" class="input-text addon addon-custom">
</p>
<div class="clear"></div>
</div><div class="count item2" style="background: none repeat scroll 0% 0% rgb(237, 97, 106); color: rgb(255, 255, 255);">2</div></div><div class="single-attendee-wrapper item3"><div class=" product-addon product-addon-three-attendees" style="display: block;">
<h3 class="addon-name">Three Attendees </h3>
<p class="form-row form-row-wide addon-wrap-414-three-attendees woocommerce-validated validate-required">
<label>Candidate Name </label> <input type="text" value="" name="addon-414-three-attendees-candidate-name" data-price="" class="input-text addon addon-custom" style="border-top: 0px none;">
</p>
<p class="form-row form-row-wide addon-wrap-414-three-attendees woocommerce-validated validate-required">
<label>Address </label> <input type="text" value="" name="addon-414-three-attendees-address" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-three-attendees woocommerce-validated validate-required">
<label>Candidate Email </label> <input type="text" value="" name="addon-414-three-attendees-candidate-email" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-three-attendees woocommerce-validated validate-required">
<label>Phone </label> <input type="text" value="" name="addon-414-three-attendees-phone" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-three-attendees woocommerce-validated validate-required">
<label>N.I Number </label> <input type="text" value="" name="addon-414-three-attendees-n-i-number" data-price="" class="input-text addon addon-custom">
</p>
<p class="form-row form-row-wide addon-wrap-414-three-attendees validate-required woocommerce-invalid woocommerce-invalid-required-field">
<label>EUSR Number </label> <input type="text" value="" name="addon-414-three-attendees-eusr-number" data-price="" class="input-text addon addon-custom">
</p>
<div class="clear"></div>
</div><div class="count item3" style="background: none repeat scroll 0% 0% rgb(237, 97, 106); color: rgb(255, 255, 255);">3</div></div><div class=" product-addon product-addon-four-attendees" style="display: none;">
</div><div data-price="29.99" data-type="simple" id="product-addons-total"></div>
<div class="quantity buttons_added"><input type="button" class="minus" value="-"><div class="quantity-wrapper" style="display: none;"><label>Number of attendees</label><input type="number" class="input-text qty text" title="Number of attendees" value="1" name="quantity" min="1" step="1"></div><input type="button" class="plus" value="+"></div>
<button class="single_add_to_cart_button button alt" type="submit" style="display: inline-block;">Place Booking</button>
</form>