Click to See Complete Forum and Search --> : help plz with validating my form


BabyDeveloper
11-04-2003, 08:43 AM
heya, howz everyone?:D
Ive used some java script to validate my coldfusion form. The form that i validated looks like this ... just to show u that its not a typical coldfusion form....

<table border=0 width=100%>
<tr>
<td colspan=2 bgcolor=silver><font face='arial' size=2>Bill To</font></td>
</tr>
<cfoutput>
<form action="checkout_getInfo.cfm" method="post" onSubmit="return checkForm(this)">

<input type="hidden" name="validatefields" id="validatefields" value="Firstname,Lastname,Phone,Fax,Email,Address1,Address2,City,State,Zip">

<input type="hidden" name="total" value="#total#">

<input type="hidden" name="gogo" value="gogo">

<cfloop list="#dbfields#" index="i">
<tr>

<td><font face="arial" size=1>#i#</font></td>

<td><input type="text" name="#i#" width=20></td>
</tr>
</cfloop>
</cfoutput>
</table>


And the java script that i used to validate it with is:

<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
<!--
function checkForm(form){
fieldsToValidate = form.validatefields.value.split(",");
for(x=0;x<form.elements.length;x++){
for(z=0;z<fieldsToValidate.length;z++){
if(form.elements[x].name == fieldsToValidate[z]){
if(form.elements[x].value == ''){
alert('Please enter a value in the field ' + fieldsToValidate[z]);
form.elements[x].focus();
return false;
}
}
}
}
}
//-->
</script>
</head>

How could i tweek the java scrip or do somthing without cahnging the form much that would allow me to validate the form more accurately...i.e. so that in the telphone field for eample u cannot enter anything but numbers and in the expiry date (in the other form) u cannot enetr anything but date format.

ANy help would be supperb!!
Thank u soo mcuh for reading this message! Yuriy

BabyDeveloper
11-05-2003, 05:40 AM
heya,
sowi just didnt want thins thread to get to the very end. does anyone know how i can accomplish this?
plz???!

Charles
11-05-2003, 06:34 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
function PhoneNumber (s) {this.number = s.replace(/^[01]/, '').replace (/([a-pr-y])/gi, function () {return {a:2, b:2, c:2, d:3, e:3, f:3, g:4, h:4, i:4, j:5, k:5, l:5, m:6, n:6, o:6, p:7, r:7, s:7, t:8, u:8, v:8, w:9, x:9, y:9}[arguments[1].toLowerCase()]}).match(/\d/g)}

PhoneNumber.prototype.toString = function () {return this.number.length == 7 ? [this.number[0], this.number[1], this.number[2], '-', this.number[3], this.number[4], this.number[5], this.number[6]].join('') : [this.number[0], this.number[1], this.number[2], '-', this.number[3], this.number[4], this.number[5], '-', this.number[6], this.number[7], this.number[8], this.number[9]].join('')}
// -->
</script>

<form action="">
<div>
<label>Phone Number<input type="text" onchange="this.value = new PhoneNumber(this.value)"></label>
<label>Expiration Date<input type="text" onchange="var d = new Date(this.value); if (isNaN(d)) {alert('That would not appear to be a valid date.'); this.value = ''; this.focus()} else {this.value = d.toDateString()}"></label>
<button type="submit">Submit</button>
</div>
</form>

BabyDeveloper
11-05-2003, 10:20 AM
Heya,
thank you for your reply! With the code that u have supplied is it possiable to keep the same form structure that i currently have? as in:

<table border=0 width=100%>
<tr>
<td colspan=2 bgcolor=silver><font face='arial' size=2>Bill To</font></td>
</tr>
<cfoutput>

<form action="checkout_getInfo.cfm" method="post>

<input type="hidden" name="total" value="#total#" required="required">

<input type="hidden" name="gogo" value="gogo">
<cfloop list="#dbfields#" index="i">
<tr>

<td>
<font face="arial" size=1>#i#</font>
</td>

<td>
<input type="text" name="#i#" width=20>
</td>
</tr>
</cfloop>
</cfoutput>
</table>

I geve the java script that i have used to validate it so far in my first post. How can i integrate another code into this to help me with my problem.
Thank you!
Yuriy