Old 11-27-2007   #1 (permalink)
Registered User
 
MAAD's Avatar
 
Join Date: Nov 2007
Posts: 10

Form Validation Help

Hi, I'm new to programming and I'm having trouble grasping certain concepts. I've done some client-side validation (for now... I plan on doing server-side validation before I go live with the form) to make sure there is input in the fields and selections before the form is processed. However, I'd like to make sure that not only are those fields passing data into the form, but also that they are passing the right kind of data (string data, numeric data) and a specified minimum and maximum amount of characters in each field. How would I go about expanding the validation in my code? I understand what my current code is doing but I don't know how to make it do a deeper validation. Would I need to create new functions or could I just add code to my current functions?

Below is an example similar to my current form and validation code.

<html>
<head>
<title>Validation Test</title>
<script language="JavaScript">
function isFilled(elm) {
if (elm.value == "" || elm.value == null) {
return false;
}
else {
return true;
}
}

function isSelected(elm) {
if (elm.value == "") {
return false;
}
else {
return true;
}
}

function submitForm() {
if (isFilled(myForm.first) == false) {
alert("Please enter something in first field.");
myForm.first.focus();
return false;
}
if (isFilled(myForm.second) == false) {
alert("Please enter something in second field.");
myForm.second.focus();
return false;
}
if (isSelected(myForm.selection1) == false) {
alert("Please choose a selection in selection 1");
myForm.selection1.focus();
return false;
}
if (isSelected(myForm.selection2) == false) {
alert("Please choose a selection in selection 2.");
myForm.selection2.focus();
return false;
}
}
</script>

</head>
<body>
<form name="myForm" method="post" action="myForm2.php" onsubmit="return submitForm(this)"

id="myForm">

<label for="first">First: </label>
<input type="text" name="first" id="first" />

<label for="second">Second: </label>
<input type="text" name="second" id="second" />

<label for="selection1">Selection 1: </label>
<select name="selection1" id="selection1">
<option value="">-Choose One-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>

<label for="selection2">Selection 2: </label>
<select name="selection2" id="selection2">
<option value="">-Choose One-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>

<input type="submit" name="submit" id="submit" value="submit" />
<input type="reset" name="reset" id="reset" value="reset" />
</form>
</body>
</html>

I appreciate any help and/or advice. Thanks!
MAAD is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-27-2007   #2 (permalink)
Janitor of Lunacy
 
tamlin's Avatar
 
Join Date: May 2006
Location: Sitting in the Wishing Chair
Posts: 4,829

I haven't used Javascript a great deal, but the following links may assist you:

JavaScript Form Validations Made Easy!

JavaScript Form Validation
__________________


Religion: It's all fun and games until someone gets burned at the stake...
tamlin is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BioRust Tutorial - Form Validation Class - Missing File phill2000star HTML / PHP / ASP / JS 3 12-20-2007 12:33 AM
Contact form help! ecntrc HTML / PHP / ASP / JS 3 01-25-2007 04:11 PM


All times are GMT +1. The time now is 10:03 PM.
Content Relevant URLs by vBSEO 3.2.0

Design & Content © BioRUST 2007 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT