NikolaiChernichenko
5/4/2016 - 8:53 AM

Простая валидация email формы

Простая валидация email формы

jQuery(function(){
	var pattern = /^[a-z0-9_-]+@[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$/i;
	var	subscribeForm = jQuery('.sailthru-newsletters-form'),
		subscribeInput = jQuery('.sailthru-newsletters-form .form-item-email input');
	
	subscribeForm.submit(function(e){
		if(subscribeInput.val() !== ''){
				if(subscribeInput.val().search(pattern) == 0){
					console.log('subsription ok')
				}else{
					subscribeForm.append('<div class="subscribe-error">Please enter a valid email address</div>');
					return false;
				}
			}else{
				subscribeForm.append('<div class="subscribe-error">Please enter a valid email address</div>');
				return false;
			}
	});
	jQuery(document).on('focus click', subscribeInput, function() {
		if(jQuery('.subscribe-error').length) {
			jQuery('.subscribe-error').remove();
		}
	})
})





// CSS
.subscribe-error {
  color: red;
  text-align: center;
  font-size: 19px;
  padding-top: 9px;
  line-height: 20px;
}