PDA

View Full Version : Couple problems with forms....


John84
01-13-2006, 04:10 AM
I apologize if these have been answered in the past but my search came up inconclusive.

1. Is there a way to set an initial value on a field but have it disappear when the user clicks on the field? ie. on a simple text field I want it to say 'Email Here Please' when the page loads but when the user clicks on it to type in his/her email, that phrase will disappear.

2. I would like to be able to validate an email address. The following code doesn't seem to work for me...
<SCRIPT language=javascript>
function validate(frm) {
var inputFields = new Array("formmail_mail_email" ,"Content");
var counter;
var name;
var msg = "Please complete the following fields:\n";
var badFields = "";
for (counter = 0; counter < inputFields.length; counter++) {
name = inputFields[counter];
if (frm.elements[name].value.length == 0) {
if (name == "formmail_mail_email") {
badFields = badFields + " - Email \n";
} else {
badFields = badFields + " - " + name + "\n";
}
}
}
if (badFields.length != 0) {
alert(msg + badFields);
return false;
}
if (frm.formmail_mail_email.value.length > 0) {
return emailCheck(frm.formmail_mail_email.value);
} else {
return true;
}
}
function emailCheck(emailStr) {
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null) {
alert("The username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray==null) {
alert("The domain name doesn't seem to be valid.");
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
alert("The address must end in a three-letter domain, or two letter country.");
return false;
}
if (len<2) {
var errStr="This address is missing a hostname!";
alert(errStr);
return false;
}
return true;
}
</SCRIPT>
... I have the text field named 'formmail_mail_email' as well as 'name="frm" onsubmit="return validate(frm)"' in the <form> tag. Will the CGI script need some recoding as well for this to work?

Any help would be very much appreciated. Thanks in advance.

Jon Hanlon
01-13-2006, 07:55 PM
<input type=text value='Email Here Please' onfocus="if (this.value == 'Email Here Please') this.value='' ">

There are plenty of email validation scripts around -- most of them a lot more straightforward -- so why not pick another one?

John84
01-14-2006, 12:24 AM
I suppose I will :) Thanks Jon

John84
01-14-2006, 08:38 PM
Been using this helpful code to confirm required fields...

onClick="
if(document.formname.inputname.value=='')
{alert('popup message');return false;}
... Is there a way to make a minimum of characters entered? I would like a popup message to occur when the value is, let's say, under 7 characters long.

aluminumpork
01-15-2006, 12:27 AM
Checking length is just as easy.



onClick="if(document.formname.inputname.value.length < 7){ alert('popup message'); return false; }



Hope that helps.

John84
01-15-2006, 01:40 PM
Worked like a charm. Thanks!