Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Validation Function Issue (js)

Featured Replies

Hi Gang,

 

Now js is not my strong point but i'm trying to pretty up a form. I've hit a bug I cannot work out. The problem is that it sort of works. On submit with all blank but a file in the img_ext and img_int fields the form it returns false and highlights fine. If you complete just name, email or phone combination of two and files in the file fields it validates fine and prevents upload. The weird bit is if you complete all but the model field it proceeds to upload regardless that the model field is blank. Any ideas?

 

The form ...

<form name="valreq" action="" method="post" class="form" id="req" enctype="multipart/form-data">
<h2 class="yk hblock"><span class="name" itemprop="name">Your Details</span></h2>
<div id="enqDets"></div>
<fieldset>
<ul class="table fs-1">
<li><label for="name">Name:</label><div><input name="name" id="name" type="text" class="input" value=""></div></li>
<li><label for="email">Email:</label><div><input name="email" id="email" type="text" class="input" value=""></div></li>
<li><label for="phone">Phone:</label><div><input name="phone" id="phone" type="text" class="input" value=""></div></li>
</ul>
</fieldset>
<div class="clear8"></div>
<h2 class="yk hblock"><span class="name" itemprop="name">Vehicle Details</span></h2>
<div id="campDets"></div>
<fieldset>
<ul class="table fs-1">
<li><label for="model">Make & Model:</label><div><input name="model" id="model" type="text" class="input"  value=""></div></li>
</ul>
</fieldset>
<fieldset>
<ul class="table fs-1">
<li><label for="img_ext">Exterior Image:</label><div><input name="imgs[]" id="img_ext" type="file"  style="background: #fff;" class="input"></div></li>
<li><label for="img_int">Interior Image:</label><div><input name="imgs[]" id="img_int" type="file" style="background: #fff;" class="input"></div></li>
</ul>
</fieldset>
<div class="center liner"><input name="submit" type="submit" class="submit yk fs2" value="Send"></div>
</form>

The validation ...

$(document.forms['valreq']).submit(sellvalidate);
function sellvalidate() {
	$("body").find('#loader').show();
	var enqForm = document.forms['valreq'];
	$('#enqDets').removeClass('errMsg').removeClass('liner').html('');
	$('#campDets').removeClass('errMsg').removeClass('liner').html('');
	$('#campSec').removeClass('errMsg').removeClass('liner').html('');
	$(enqForm).find('input, label, select, div').removeClass('errbox');
	if(enqForm.name.value=="") {
		$(enqForm.name).parent('div').addClass('errbox');
		$(enqForm.name).parent('div').prev('label').addClass('errbox');
		$(enqForm.name).focus();
		$('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your name.');
		$("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000);
		$("body").find('#loader').hide();
    	return false;
	};
	if(enqForm.email.value=="") {
		$(enqForm.email).parent('div').addClass('errbox');
		$(enqForm.email).parent('div').prev('label').addClass('errbox');
		$(enqForm.email).focus();
		$('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your email.');
		$("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000);
		$("body").find('#loader').hide();
    	return false;
	} else {
		var emt = $(enqForm.email).val();
   		email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
   		if(!email_regex.test(emt)){ 
			$(enqForm.email).parent('div').addClass('errbox');
			$(enqForm.email).parent('div').prev('label').addClass('errbox');
			$(enqForm.email).focus();
			$('#enqDets').addClass('errMsg').addClass('liner').html('Please provide a valid email address.');
			$("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000);
			$("body").find('#loader').hide();
    		return false;
		}
	};
	if(enqForm.phone.value=="") {
		$(enqForm.phone).parent('div').addClass('errbox');
		$(enqForm.phone).parent('div').prev('label').addClass('errbox');
		$(enqForm.phone).focus();
		$('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your phone number.');
		$("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000);
		$("body").find('#loader').hide();
    	return false;
	} else {
		var phone = $(enqForm.phone).val();
    	intRegex = '/[0-9 -()+]+$/';
		if((phone.length < 6) || (!intRegex.test(phone))) {
    		$(enqForm.phone).parent('div').addClass('errbox');
			$(enqForm.phone).parent('div').prev('label').addClass('errbox');
			$(enqForm.phone).focus();
			$('#enqDets').addClass('errMsg').addClass('liner').html('Please provide a valid phone number.');
			$("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000);
			$("body").find('#loader').hide();
    		return false;
		}
	};
	if(enqForm.model.value=="") {
		$(enqForm.model).parent('div').addClass('errbox');
		$(enqForm.model).parent('div').prev('label').addClass('errbox');
		$(enqForm.model).focus();
		$('#campDets').addClass('errMsg').addClass('liner').html('Please provide the vehicle make and model.');
		$("html, body").animate({ scrollTop: $('#campDets').offset().top - 20 }, 1000);
		$("body").find('#loader').hide();
    	return false;
	};
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.