Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:36 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: AJAX form validation
PostPosted: Mon Aug 17, 2009 1:43 pm 
Offline
Newbie
User avatar

Joined: Fri Jun 26, 2009 5:50 am
Posts: 23
Has thanked: 0 time
Have thanks: 1 time

Hi,
I am currently validating a form client side only using javascript. People have started submitting blank forms by disabling javascript in there browser so i would now like to add some server side validation. Here is the code i am using. Any suggestions are welcome.

Thanks

<script type="text/JavaScript">
<!--
function ValidateForm (FormName) {
// check that a title has been selected
if (FormName.Title.selectedIndex < 0)
{
alert("Error - You must select a title");
FormName.Title.focus();
return (false);
}

// check if the first drop down is selected, if so, invalid selection
if (FormName.Title.selectedIndex == 0)
{
alert("Error - You must select a title");
FormName.Title.focus();
return (false);
}

// check to see if the first name is blank
if (FormName.FirstName.value == "")
{
alert("Error - You must enter a first name.");
FormName.FirstName.focus();
return (false);
}

// check to see if the first name is blank
if (FormName.Surname.value == "")
{
alert("Error - You must enter a surname.");
FormName.Surname.focus();
return (false);
}

// check if email field is blank
if (FormName.Email.value == "")
{
alert("Error you must enter a valid email address.");
FormName.Email.focus();
return (false);
}

// Check email address contains @ and .
var chkEmail = "@.";
var chkStr = FormName.Email.value;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
for (i = 0; i < chkStr.length; i++)
{
ch = chkStr.charAt(i);
for (j = 0; j < chkEmail.length; j++)
{
if (ch == chkEmail.charAt(j) && ch == "@")
EmailAt = true;
if (ch == chkEmail.charAt(j) && ch == ".")
EmailPeriod = true;
if (EmailAt && EmailPeriod)
break;
if (j == chkEmail.length)
break;
}
// Found @ and . in the email address
if (EmailAt && EmailPeriod)
{
EmailValid = true
break;
}
}
if (!EmailValid)
{
alert("Error - The email address must contain a @ and a .");
FormName.Email.focus();
return (false);
}
// check to see if the contact telephone is blank
if (FormName.Telephone.value == "")
{
alert("Error - You must enter a contact telephone number.");
FormName.Telephone.focus();
return (false);
}

// check that the agree terms has been selected
if (!FormName.AgreeTerms.checked)
{
alert("Error - You must agree to the terms and conditions");
return (false);
}
// check to see thatthe spam filter has been entered
if (FormName.SpamFilter.value == "")
{
alert("Error - Please enter the spam filter ");
FormName.SpamFilter.focus();
return (false);
}
// check to see that the spam filter is correct
if (FormName.SpamFilter.value != "19")
{
alert("Error - Incorect answer for the spam filter 20+6-7=");
FormName.SpamFilter.focus();
return (false);
}
return (true);
}
//-->
</script>

_________________
Thanks & regards
Lokananth
[url "http://www.mioot.com"]Live Chat Software[/url] By miOOt


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Sending One lakh character in ajax  AJAX  Anonymous  0
 getting gst, pst total to diplay in my javascript form  Scripting Language  Anonymous  0
 Ajax Source code to Suggest application with JSP Server side  AJAX  msi_333  5
 [Ajax/PHP] Registration - Check for empty textboxs  AJAX  PF2G  0
 How to make PHP form data saved into txt file  PHP  Radu  6

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team