473,657 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form validation. Help!!!

27 New Member
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(theEle ment) {
if (theElement.val ue == "") {
theElement.styl e.background = "#ffcccc";
return false;
}
return true;
}

function is_phone(target ) {
target.value = trim(target.val ue);
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.ba ckground = "#ffcccc";
return false;
}

function is_email(target ) {
target.value = trim(target.val ue);
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.ba ckground = "#ffcccc";
return false;
}

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

//this will get rid of trailing spaces
while (strText.substr ing(strText.len gth-1,strText.lengt h) == ' ')
strText = strText.substri ng(0, strText.length-1);

return strText;
}

function writeError(theE rror) {
var id = "error";
var x;
if(document.get ElementById) {
x = document.getEle mentById(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.backgrou nd = "white";
}
if (!hasValue(thef orm.fname))
noError = false;
if (!hasValue(thef orm.phone) ||!is_phone(the form.phone))
noError = false;
if (!hasValue(thef orm.email) || !is_email(thefo rm.email))
noError = false;
if (!noError) {
if (theError)
writeError(theE rror);
else
writeError("*Pl ease fill in all required fields.");
}
return noError;
}
</script>
</head>
<body >
<!--Start root -->
<div >
<?php
if(isset($_POST["form"])) {

$fname = htmlspecialchar s($_POST["fname"]);
$phone = htmlspecialchar s($_POST["phone"]);
$email = htmlspecialchar s($_POST["email"]);
?>
<div>
<fieldset >
<legend>Model Call Sign Up</legend>
<p>*Form validates</p>
<p><span>Firs t 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">Fir st Name:</label><input type="text" id="fname" name="fname" value="" /></p>
<p><label for="phone">Pho ne:</label><input type="text" id="phone" name="phone" value="" /></p>
<p><label for="email">Ema il:</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 1689
phvfl
173 Recognized Expert New Member
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
bdbeames
27 New Member
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 Recognized Expert New Member
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
1235
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 another window and ask user why they left it blank. I then want to save that "why data" in an access database table. I don't want to force them to put in age, but I need to know why they skipped the field.
4
2629
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 script that validates the form fields. the validation procedure is called ONCLICK event of the submit button. Follwowing is the structure of the validation procedure.
5
1279
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 function that checks for each of these and still validates the rest as a number: function checkNumber( an_item ) { count_val = an_item.value;
2
1636
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 program. I would like to interactively validate the data as they are entering it, rather than waiting until they submit the form. So, I am doing something like this (this is a very simple example): <head>
9
4167
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 honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
14
2141
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 the next one to be sure I am correct. I ran into a problem with my validation when I added an else if code to my code. Here is what I tried to do: Form (ITTermination) has a field (EmployeeName) which I would like to validate to check for no...
2
1399
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 the value of the first 4 text boxes is either "0" or greater than 40 - the default of these is "0" so I thought a validation onChange() would help here??
0
1334
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 the following so that the form is not submitted unless the following is true. 1. The "name" field must be maximum 20 characters. Not left empty. Letters only. 2. The "email" must be a valid email. Not left empty. 3. The "subject" must be maximum...
2
1635
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 save button. Simply because it is often easier to explain to people just click save and it will save the record than it is for them to grasp the idea that access doesnt need a record to be "saved" from a button. In the click event of this save...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.