Welcome to WebmasterWorld Guest from

Forum Moderators: incrediBILL

Message Too Old, No Replies

HTML form validation - html form code

How to ensure users type the correct responses

2:40 am on Sep 4, 2011 (gmt 0)

New User

5+ Year Member

joined:Aug 18, 2011
posts: 4
votes: 0

Hi,I am a newbie who uses third party software to create websites. So I have this website and then i learned how to create a form. Never took html course so bare with me. The bits I know have brought me far. Only thing is I don't know how to make sure users enter the right responses. For example, in the email field, you can enter anything and submit it. I have no code or way to make sure it's actually an existing email. You can enter random numbers and still no validation. I'm not good at CSS so the challenge is, no CSS. I used a third party form software but it screws my W3C html validation. Any help? Any input pleease?

Here is the code:

<form method="post" action="http://www.mydom.com/cgi-bin/formmail.pl">
<input type="hidden" name="redirect" value="http://www.mydom.com/thankyoupage.html"/>
<input type="hidden" name="recipient" value="newsletter@mydom.com"/>
<input type="hidden" name="subject" value="Newsletter or quote"/>
<div>*Your Name:<input type="text" name="name" size="25" maxlength="100"/>
<div>*Email:<input type="text" name="email" size="25" maxlength="140"/>
<div>*Phone Number: <input type="text" name="phone" size="25" maxlength="140"/>
<div>Company:<input type="text" name="Company" size="25" maxlength="140"/>
<div>*Website Address: <input type="text" name="URL" size="25" maxlength="140"/>
<div>Monthly Budget:<input type="text" name="Bugget" size="25" maxlength="140"/>
<div>How did you find us?<input type="text" name="Bugget" size="25" maxlength="140"/>
<div align="right">*This request is to
<select name="request for">
<option value="newsletter">Join our newsletter</option>
<option value="quote">FREE quote </option>
<option value="quote">Call Back (specify time/goal box) </option>
<option value="other">Other (please specify below)</option>
<div align="left">Owner?
<select name="ownership">
<option value="own">Yes</option>
<option value="do not own">No</option>
<div align="left">Your Goal: <div>
<textarea name="comments" rows="6" cols="35">
<div align="left">
<input type="submit" name="Request" value="submit"/>
<input type="reset" name="Clear" value="clear form"/>

There is no way of validating any of my fields. How do i make sure the email address is actually an email address and that the website address is actually a website URL?
4:28 am on Sept 4, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 598

Uhm, I hope you simply mean "correctly formatted" not "actually valid" because that takes you into a whole nother realm. You want something in the form blahblah at blahblah dot tld, and then something in the form {optional www.} blahblah dot tld, right?

Short answer: You cannot do it with html alone. But take heart: CSS won't do anything either; that's just for presentation.

Piece of cake in javascript, php or language of your choice. Betcha if you keep looking, you'll find a plugin that lets you constrain responses to a given format. Another common one is telephone numbers.

Oh, and if your generated html doesn't validate, find a different package.
5:51 am on Sept 4, 2011 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 126

you can use Document Object Model Events:
http://www.w3.org/TR/DOM-Level-2-Events/events.html [w3.org]
(such as onchange, onselect, onsubmit) to trigger JavaScript Form Validation:
http://www.w3schools.com/js/js_form_validation.asp [w3schools.com]

this browser-side validation should help prevent the POST request from being sent until some reasonable input validation is executed.
you must also do similar and usually additional input validation and sanitizing in your form action script.
4:10 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

To add to that last bit - Javascript can be disabled, which means that no matter what you do client side via Javascript - which is actually an enhancement in the interest of the user, not a way to avoid abuse of your script - you'll have to duplicate that server side (I.E., within the receipt at "formmail.pl")

If this is the old script from M.S.A., I'd recommend changing over to NMS Form Mail (google it) which leads to a Pretty Good Answer to the validation question: the NMS formmail has pretty robust instructions on how to manage some of the form inputs, particularly email.
4:37 pm on Sept 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Aug 18, 2011
posts: 4
votes: 0

Will try everything here. Thank you guys.