mi*****@hotmail.com wrote:
thanks for the help, I'll get to it.
Michael
Here is a head start - I missled you on the return true thing. If
you don't sepecify a value, functions that complete return
'undefined', which is not false (duh!) unless you specify otherwise.
The script below provides a skeleton to get going, use what you like.
Lightly commented, tested in Safari and Firefox on Mac OS X.
<HTML>
<head><title>stuff</title></head>
<BODY>
<script type="text/javascript">
function validateForm(f){
var ph = f.phone.value;
var ccn = f.CCName.value;
var yr = f.CCEYear.value;
var msg = [];
var t;
// Test phone number
// Format is 8 digits
if ( (t=testPH(ph)) && 'undefined' != typeof t ) {
msg.push(t);
}
// Test credit card name
// Must be more than 5 characters without spaces
if ( (t=testCCN(ccn)) && 'undefined' != typeof t) {
msg.push(t);
}
// Test year
// must not be 2006 or not a number (see note below)
if ( (t=testYR(yr)) && 'undefined' != typeof t) {
msg.push(t);
}
// If we got any errors, write them & return false
if ( 0 != msg.length ){
writeMsg(msg.join('<br>'));
return false;
}
}
function testPH(p) {
// remove spaces, check if 8 digits and 8 long
// you may want a different test here
p = p.replace(/\s*/g,'');
if (!/\d{8}/.test(p) || 8 != p.length ) {
return '<b>Phone number must be 8 digits</b>';
}
}
function testCCN(c) {
// remove spaces, check if 5 characters of any kind
// you may want a better test here
var x = c.replace(/\s*/g,'');
if (x.length < 5) {
return '<b>Name must be at least 5 characters,'
+' not counting the spaces</b>';
}
}
function testYR(y) {
// check if y is 2006 or not a number
// Note: only use isNaN here because value is from conrolled list
// Otherwise more robust validation is required.
if ( 2006 == y || isNaN(y) ) {
return '<b>Year can\'t be ' + y + '</b>'
}
}
function checkForm(f) {
// Run validate from a button rather than onclick
if ('undefined' == typeof validateForm(f)){
var txt = 'Values are all OK';
writeMsg(txt)
}
}
function clearErr(f){
// Just clears error messages
writeMsg('');
}
function writeMsg(m){
// Generic write function. If getElementById not supported,
// Presents error in an alert after repalcing <br> with \n
// and removing all other markup
if (document.getElementById) {
document.getElementById('eSpan').innerHTML = m;
} else {
alert(m.replace(/<br>/g,'\n').replace(/<[^<>]*>/ig,''));
}
}
</script>
<H1>Order Form</H1>
<HR>
<FORM NAME="form1" action="" onSubmit="return validateForm(this);
return false;">
<table border="0">
<tr>
<td>Phone</td>
<td>
<INPUT TYPE="TEXT" NAME="phone" SIZE="25"
value="1234 5678"><br>
<i>nnnn nnnn</i>
</td>
</tr>
<tr>
<td>CCName</td>
<td>
<INPUT TYPE="TEXT" NAME="CCName" SIZE="30"
value="asdfasdf"><br>
<i>Must be more than 5 characters</i>
</td>
</tr>
<tr>
<td>Card ExpiryYear</td>
<td>
<select name="CCEYear">
<option SELECTED value="default">Select Year</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
</select>
</td>
</tr>
</table>
<INPUT TYPE="button" VALUE="check form"
onclick="checkForm(this.form);">
<INPUT TYPE="reset" onclick="clearErr(this.form);
this.form.reset();">
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</FORM>
<span id="eSpan"></span>
</BODY>
</HTML>
--
Fred