homepage Welcome to WebmasterWorld Guest from 54.227.25.58
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Using a form validator
need help validation a form with values already inside the form
togethercomms




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

Hi,

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)


<script>
function checkForm() {
blogid = document.getElementById("name").value;
if (name == "") {
hideAllErrors();
document.getElementById("nameError").style.display = "inline";
document.getElementById("name").select();
document.getElementById("name").focus();
return false;
}
return true;
}

function hideAllErrors() {
document.getElementById("nameError").style.display = "none"
}
</script>

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

 

Arno_Adams




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

Hi,

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>
</form>

HTH, Tom

togethercomms




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

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

Arno_Adams




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

Sure,

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?

Tom

togethercomms




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

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

Many Thanks

Arno_Adams




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

Still doesn't make sense. Show us what your form looks like.

togethercomms




msg:3971915
 1:49 pm on Aug 14, 2009 (gmt 0)

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">
</form>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved