/    Sign up×
Community /Pin to ProfileBookmark

Form Validation help

Hi I’m trying to create a simple registration form with ajax validation. I’ve got the form working almost how I’d like… a message appears on the page with the errors for each field entered incorrectly.

The problem is all the error messages appear in one div together and I would like to have each field have its own div containing only its own error.

I think I need to wrap my error messages in xml and use something like this –

[code]document.getElementById(“feedback1”).innerHTML= xmlDoc.getElementsByTagName(“lname”)[0].childNodes[0].nodeValue;[/code]

– but can’t seem to get it working

Does anyone know how I should modify my code to do this?

Attached are the 3 I’m currently using

And Here is my code for my ajax page only.

[code]
var xmlHttp;

function regForm(thisform) {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert (“Browser does not support HTTP Request”);
return;
}
var formdata = “”;
formdata = “lname=” + thisform.elements[‘lname’].value + “&fname=” + thisform.elements[‘fname’].value + “&email=” + thisform.elements[’email’].value + “&username=” + thisform.elements[‘username’].value + “&pass=” + thisform.elements[‘pass’].value + “&pass2=” + thisform.elements[‘pass2’].value;
xmlHttp.onreadystatechange=formSubmitted;
xmlHttp.open(“POST”, “adduser.php”,true);
xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xmlHttp.setRequestHeader(“Content-length”, formdata.length);
xmlHttp.setRequestHeader(“Connection”, “close”);
xmlHttp.send(formdata);
return false;
}

function formSubmitted() {
if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”) {
xmlDoc=xmlHttp.responseXML;
document.getElementById(“feedback”).innerHTML = xmlHttp.responseText;
//document.getElementById(“feedback1”).innerHTML= xmlDoc.getElementsByTagName(“lname”)[0].childNodes[0].nodeValue;
}
}

function GetXmlHttpObject() {
xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
[/code]

[upl-file uuid=dc3dfc63-934b-4cb1-b60c-0ef803b78d90 size=4kB]ajax-validation.zip[/upl-file]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 27.2010 — What is the format of responseText?

Where are the error messages displayed exactly?
Copy linkTweet thisAlerts:
@cultivateauthorJan 27.2010 — What is the format of responseText?

Where are the error messages displayed exactly?[/QUOTE]


The response Text is just being echoed out in a php page. And I have the validation error messages displaying all in one div on the form page so when the user hit the submit button they stay on the page, their form data is not lost and the error messages appear... I would just like to have each error message appear in its own div positioned next to its relevant form field.

any thought.
Copy linkTweet thisAlerts:
@FangJan 27.2010 — Split the errors in responseText. This would then write the error for [I]lname[/I] or an asterisk (as in the present form)
document.getElementById('lname').parentNode.lastChild.data = (errortextForLname)? errorTextForLname : '*'Similar code is required for each validated form control.
Copy linkTweet thisAlerts:
@cultivateauthorJan 27.2010 — Thanks for the insight fang, but I've almost got it working how I'd like...

I've got an error message coming back in its own div, but when I try to send multiple messages back nothing happens, any thoughts?

Here's a bit of my js code

document.getElementById("feedback1").innerHTML= xmlDoc.getElementsByTagName("lname")[0].childNodes[0].nodeValue;
document.getElementById("feedback2").innerHTML= xmlDoc.getElementsByTagName("fname")[0].childNodes[0].nodeValue;


and here is my simple adduser.php page so far
<?php
header('Content-Type: text/xml');

$lname = mysql_real_escape_string($_POST['lname']);
//$fname = mysql_real_escape_string($_POST['fname']);

if($lname == NULL) {
echo "<lname>NEED TO FILL</lname>";
}

//if($fname == NULL) {
//echo "<fname>NEED TO FILL</fname>";
//}
else {
echo "<lname> </lname>";
//echo "<fname> </fname>";
}
?>


As you can see I've got the fname information commented out right now and my messaging is working for lname but as soon as I uncomment the fname stuff in hopes to send a message for both lname and fname nothing happens I don't understand why.
Copy linkTweet thisAlerts:
@FangJan 27.2010 — Surely you should return 1 string?
Copy linkTweet thisAlerts:
@cultivateauthorJan 27.2010 — Surely you should return 1 string?[/QUOTE]

you mean something a little more like this?

<i>
</i>header('Content-Type: text/xml');
$errors = array();

$lname = mysql_real_escape_string($_POST['lname']);
//$fname = mysql_real_escape_string($_POST['fname']);

if($lname == NULL) {
$errors[] = "&lt;lname&gt;NEED TO FILL&lt;/lname&gt;";
}

$errmsg = "";

$errcount = count($errors);
if($errcount &gt; 0) {
echo "The following errors occurred when your form was submitted:&lt;br /&gt;&lt;br /&gt;";
for($i=0; $i&lt; $errcount; $i++) {
$errmsg .= $errors[$i]."&lt;br /&gt;";
}
echo $errmsg;
}

else {
echo " ";

}


This is what I had to start with but I can't seem to get it working I'd really like to have the individual error messages returned as XML and displayed in separate divs.
Copy linkTweet thisAlerts:
@FangJan 27.2010 — Split the responseText using JavaScript.
×

Success!

Help @cultivate spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...