Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Using a form validator

need help validation a form with values already inside the form

8:25 am on Aug 14, 2009 (gmt 0)

5+ Year Member


I have a form that already has values describing what the boxes are, inside them.

I need a form validator that can detect whats a default and what the user has put in.

The code i have already i can't figure out and somehow i have managed to get it working on another website.

I have 4 fields which need validating; name, telephone, email, contact.

is their any other way of doing this?
Any Help is appreciated (The exsisting code is below)

function checkForm() {
blogid = document.getElementById("name").value;
if (name == "") {
document.getElementById("nameError").style.display = "inline";
return false;
return true;

function hideAllErrors() {
document.getElementById("nameError").style.display = "none"

This works on only 1 field and only if that field is empty

9:05 am on Aug 14, 2009 (gmt 0)

10+ Year Member


try this

function f(o) {
var b = {name: 'naam', telephone: 'telefoon', email: 'e-mail', contact: 'Contact'};

for(var i in b) {
if(document.getElementById(i).value == b[i]) {
alert('ERROR'); // Or do whatever you like...
return false;
return true;

And this form looks like this:

<form id="frm" method="post" action="/" onsubmit="return f(this);">
<input type="text" name="name" id="name" value="naam" />
<input type="text" name="telephone" id="telephone" value="telefoon" />
<input type="text" name="email" id="email" value="e-mail" />
<input type="text" name="contact" id="contact" value="Contact" />
<button type="submit">Go</button>

HTH, Tom

11:03 am on Aug 14, 2009 (gmt 0)

5+ Year Member

would it be possible if you could walk me through this code, i'm quite new to this and their are somethings that i don't understand.

Also the contact class are radio boxes, will that matter?

Many Thanks

11:41 am on Aug 14, 2009 (gmt 0)

10+ Year Member


var b is an object that contains pairs of name/value;

The name is the form field id, the value is the default value for the form field.

Next we loop through these pairs. If the value in the form fields is identical to the value in the object you'll see an error msg. If not, the values in the form are not identical to the default value and the function will return 'true'.

And I'm not sure what you mean with

he contact class are radio boxes

Do you mean the form fields are radio boxes?


12:42 pm on Aug 14, 2009 (gmt 0)

5+ Year Member

yh the class called "contact" are radio boxes, i was wondering if that matters through the validation?

Many Thanks

12:44 pm on Aug 14, 2009 (gmt 0)

10+ Year Member

Still doesn't make sense. Show us what your form looks like.
1:49 pm on Aug 14, 2009 (gmt 0)

5+ Year Member

this is the code i have at the moment, hope it all makes sence

<form onSubmit="return checkForm();" action="php/mailto.php" method="POST">
<input onfocus="this.select()" type="text" name="name" id="name" class="firstname" value="Full Name"><br>

<input onfocus="this.select()" type="text" name="jobtitle" id="jobtitle" class="jobtitle" value="Current Job Title"><br><br>
<input onfocus="this.select()" type="text" name="employer" id="employer" class="employer" value="Current Employer"><br><br>
<input onfocus="this.select()" type="text" name="tel" id="tel" class="tel" value="Telephone Number"><br>

<input onfocus="this.select()" type="text" name="email" id="email" class="email" value="Email Address"><br>

I would like a Consultant to contact me for a conversation?<br>
<div class="radiobox"><input type="radio" id="yes" class="contact" value="Yes">Yes &nbsp; &nbsp; &nbsp; <input type="radio" id="no" class="contact" value="No">No<br>

<input onfocus="this.select()" type="submit" name="submit" id="submit" class="submit" value="Submit Form">