473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Form validation. Help!!!

Ok I have a form validation problem that I need one of you javascript ninja s to help me with.

The following is a form with the javascript that I have used for the last year or two to validate. It works quite nicely and I like the way it works. The problem is I am working on a site where the html needs to validate using STRICT. This form doesn't validate with STRICT because I use a name on the form and not and id. Could someone show me how to do this so that my page will validate using STRICT. I assume you change the name to id on the form, but then I can't then get the javascript to work right.

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
function hasValue(theElement) {
if (theElement.value == "") {
theElement.style.background = "#ffcccc";
return false;
}
return true;
}

function is_phone(target) {
target.value = trim(target.value);
var filter = RegExp("^[1-9]{1}[0-9]{2}\-[0-9]{3}\-[0-9]{4}$");
if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
return true;
target.style.background = "#ffcccc";
return false;
}

function is_email(target) {
target.value = trim(target.value);
var filter = RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
return true;
target.style.background = "#ffcccc";
return false;
}

function trim(strText) {
//this will get rid of leading spaces
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);

//this will get rid of trailing spaces
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);

return strText;
}

function writeError(theError) {
var id = "error";
var x;
if(document.getElementById) {
x = document.getElementById(id);
x.innerHTML = "";
x.innerHTML = theError;
} else if (document.all) {
x = document.all[id];
x.innerHTML = theError;
}
}

function formCheck() {
var noError = true;
var theform = document.Form;
var theError = "";
var len = theform.length;
for (var n=0; n < len; n++) {
if ((theform[n].type == "text"))
theform[n].style.background = "white";
}
if (!hasValue(theform.fname))
noError = false;
if (!hasValue(theform.phone) ||!is_phone(theform.phone))
noError = false;
if (!hasValue(theform.email) || !is_email(theform.email))
noError = false;
if (!noError) {
if (theError)
writeError(theError);
else
writeError("*Please fill in all required fields.");
}
return noError;
}
</script>
</head>
<body >
<!--Start root -->
<div >
<?php
if(isset($_POST["form"])) {

$fname = htmlspecialchars($_POST["fname"]);
$phone = htmlspecialchars($_POST["phone"]);
$email = htmlspecialchars($_POST["email"]);
?>
<div>
<fieldset >
<legend>Model Call Sign Up</legend>
<p>*Form validates</p>
<p><span>First Name:</span><?php echo $fname; ?></p>
<p><span>Phone:</span><?php echo $phone; ?>
</p>
<p><span>Email:</span><?php echo $email; ?></p>
</fieldset>
</div>
<?php
} else {
?>
<div>
<form name="Form" action="" method="post" enctype="25" >
<fieldset>
<legend>Model Call Sign Up</legend>
<div id="error"></div>
<p><label for="fname">First Name:</label><input type="text" id="fname" name="fname" value="" /></p>
<p><label for="phone">Phone:</label><input type="text" id="phone" name="phone" value="" /></p>
<p><label for="email">Email:</label><input type="text" id="email" name="email" value="" /></p>
<p id="button">
<input type="submit" name="form" value="Sign Up" onclick="if (formCheck() == false) return false;" />
<input type="reset" />
</p>
</fieldset>
</form>
</div>
<?php
}
?>
</div>
</body>
</html>
[/html]

Thanks for the help.
Dec 1 '07 #1
3 1670
phvfl
173 Expert 100+
Hi,

If it is only the name on the form that you need to change then the easiest way that I can see to make the script work is to change the line that sets your 'theform' variable (line 64 on the posted code) to:
Expand|Select|Wrap|Line Numbers
  1. var theform = document.getElementById('Form');
  2.  
and set the ID of the form to 'Form' , the same as the name currently is.
Dec 1 '07 #2
Ok, now I feel really stupid.

You solved all my problems. Don't know how long I played this. I tried that before, but I had it typed in wrong.

thanks again.
Dec 1 '07 #3
phvfl
173 Expert 100+
No problem. Don't know if it was your problem but if something does not seem to be working check the case that you have used as Javascript is case sensitive. Also if you use Firefox check out the Firebug extension as it provides javascript debugging and console which are very useful.
Dec 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dave | last post by:
Hi, I would like to know if this scenario is possible using Javascript. Form has input field such as Age, weight etc. If a user does not put in data in the field, I would like to open...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
5
by: Iain Downie | last post by:
Dear Group, I have a text field that I wish to cellect numbers of bird seen. However, some folk want to use 'c' before the number or '+' after to indicate a fuzzy number. I have tried creating a...
2
by: Vinny | last post by:
A customer wants me to convert a VB app I wrote for them into a web app for remote users to do data entry. So, I'm writing a small ASP program which prompts the user for data, much like a VB...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
14
by: JNariss | last post by:
Hello, I am fairly new to asp and jscript and am now in the process of learning some form validation. I am taking one step at a time by validating each field and testing it before moving onto...
2
by: argylew | last post by:
This is simple, SO simple but I dont know how to do it!! I have 5 text boxes on one page (all contain numbers). The 5th box is the sum of the first four. I need to validate it so that the...
0
by: karen987 | last post by:
This is an email form on an ASP page. I want to add validation before it submits, The current validation only checks "name, email, and content" (server side) if the spaces are empty. I need to add...
2
by: munkee | last post by:
Hi all, Basically I have been using form validation incorrectly. Partly because of laziness which I now feel is going to really bite me back if I dont get it sorted. On all of my forms I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.