Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How to make this form mandatory?
Old 01-05-2012, 09:23 AM How to make this form mandatory?
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Hi,

I am using a simple "add email address" form but need to make the field so it can't be submitted without adding an email address??

Can anyone advise please...?

Code:
<form method="POST" action="news.php">

				<input class="newsletter_input" name="Email" id="s" value="enter your email here..." onfocus="if(this.value == 'enter your email here...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'enter your email here...';}" type="text" />

				<input name="" value="" class="newsletter_submit" type="submit" />
					
			</form>
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-05-2012, 09:36 AM Re: How to make this form mandatory?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
disable the submit button until the email validates.

http://www.w3schools.com/js/js_form_validation.asp

use the onBlur event of the email field to trigger the check.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-05-2012, 09:52 AM Re: How to make this form mandatory?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Or just use required fields, in HTML5.

HTML Code:
<form method="post" action="news.php">
<input required class="newsletter_input" name="Email" id="s" value="enter your email here..." type="text" />
<input value="Go" class="newsletter_submit" type="submit" />				
</form>
Notice the required in the second line.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 01-05-2012, 12:07 PM Re: How to make this form mandatory?
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Thanks Guys,

javascript is not my favourite subject...!

For any one who is interested, I solved it by using

HTML Code:
<form name="news" action="news.php" onsubmit="return validateForm()" method="post"><input name="Email" type="text" class="newsletter_input"> 
<input type="submit" class="newsletter_submit" value="">
</form> 
and placing in the head section...

HTML Code:
<script type="text/javascript">
function validateForm()
{
var x=document.forms["news"]["Email"].value;
if (x==null || x=="")
  {
  alert("Please Enter Your Email Address!");
  return false;
  }
}
</script>
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 01-05-2012, 04:52 PM Re: How to make this form mandatory?
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Just a reminder that you still need to check on the backend that an email address was submitted. There are a couple of ways to still have the form submitted even with the javascript check.

1. Disable javascript
2. Copy the source code, remove the onsubmit, and submit form

I'm guessing the validation is more for the user than you, but if you absolutely need the email you can't rely on front end validation. Don't forget the backend.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-05-2012, 05:04 PM Re: How to make this form mandatory?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by vangogh View Post
Just a reminder that you still need to check on the backend that an email address was submitted. There are a couple of ways to still have the form submitted even with the javascript check.

1. Disable javascript
2. Copy the source code, remove the onsubmit, and submit form

I'm guessing the validation is more for the user than you, but if you absolutely need the email you can't rely on front end validation. Don't forget the backend.
Yep! All you'd need to do is have a little if(isset($_POST['Email'])&&$_POST['Email']!=null) to make sure that the user really DID put something in the box instead of hacking their way through your Javascript. You can also run a email filter on the string to make sure it's an email, too. And if you REALLY want to go crazy, checkdnsrr will tell you whether or not the domain exists (gmail.com, yahoo.com, hotmail.com, etc).
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to make this form mandatory?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.43514 seconds with 12 queries