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.

Javascript

Featured Replies

<script type="text/javascript">
<!--
function validate_form_1( form )

{
	if( form.elements['name'].value=="" ){alert("You must enter you're name");form.elements['name'].focus();return false;}
 return false;
} 
-->

</script>

 

Above is some javascript to check if my name inputbox is empty or not, it works fine,

 

{
	if ( form.elements['email'].value != ['emailconfirm'].value){alert("You're Emails are incorrect");form.elements['email'].focus();
		return false;
	}
return true;
}

 

Above this is the code to check the emails, this works on its own as well, ive tried multiple ways of adding them together, putting an else between them and taking the return true from the first statement, ive searched google, but i cant seem to find anything.

 

Any help is really appreciated :D

I use a slightly different method to yours (I use "getElementById"), here's my code:

 

<script type="text/javascript">
/* <![CDATA[ */

function checkform (mailform)
{
	// Name
	if (document.getElementById('mailform').name.value == "") {
		alert( "Please enter your name." );
		document.getElementById('mailform').name.focus();
		return false;
	}

	// Contact Number
	if (document.getElementById('mailform').contact_number.value == "") {
		alert( "Please enter your contact number." );
		document.getElementById('mailform').email.focus();
		return false;
	}

	// Email Address
	if (document.getElementById('mailform').email.value == "") {
		alert( "Please enter your email address." );
		document.getElementById('mailform').email.focus();
		return false;
	}

	// Message
	if (document.getElementById('mailform').message.value == "") {
		alert( "Please enter your message." );
		document.getElementById('mailform').message.focus();
		return false;
	}
return true;
}

/* ]]> */
</script>

 

There's probably a better way of doing the above but my code works so as they say, if it ain't broke don't fix it. :)

  • Author

Thanks for the reply, ill have a look and try it and out, fingers crossed :)

 

Thanks again :)

  • Author

Quick update, i got it working like a beauty :D

 

Thanks Mate :D :D

 

If i wanted to put this into a .js file how would i go about naming it?

 

and

	if (document.getElementById('form_1').email.value != document.getElementById('form_1').confirmemail.value) {
		alert( "Please make sure your email address match." );
		document.getElementById('form_1').email.focus();
		return false;
	}

i got that to check the emails match, but after i click ok on the alert box the form refreshes, it doesnt do this on the rest, have i changed something or is it because im doing a statement to see if they match?

 

Thank you so much for the help so far, been ripping my hair out for hours over this. :)

I assume you could just copy and paste the function into a new file and save it as a .js file, then call it from your HTML page.

 

My function doesn't check everything at once it just fails on the first thing that isn't filled in. When I get a chance I will probably rewrite it so it shows up all errors in one go.

  • Author

Ill have a look into it and if i find anything out ill let you know :)

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.