Tech Support Guy banner

Forms field format

1442 Views 10 Replies 3 Participants Last post by  lola2001
Is there a way to make a form field that will verify that the correct information is typed in. For instance I have a form field that asks for a phone number, including the area code and also a field that asks for an email address. I want to make sure that the correct format is typed in and if nothing is typed in those fields then the page should be refreshed with a message saying they missed a field. Am I making any sense here? Thanks.

I'm using Dreamweaver.
Status
Not open for further replies.
1 - 11 of 11 Posts
You need to write a java function to validate the form elements and that will fire when the form is dubmitting using the forms onSubmit event. Here's a simple one for an email address

PHP:
function ValidateForm(formname)
if(form.Email.value == "")
	{ 
		alert('You must type in an Email address') 
		formname.Email.focus();
		return false;
} 
else if(form.Email.value.indexOf('@')== -1)
	{
		alert('Your Email address is missing an @ symbol') 
		formname.Email.focus();
		return false;
	//if the EmailAddrs does not contain a full stop
	}
else if(form.Email.value.indexOf('.')== -1)
	{
		alert('Your Email address is missing a period') 
		formname.Email.focus();
		return false;
	}
See less See more
Ok, thanks. I'll use that. Shouldn't there be something in Dreamweaver that would make it easier though?
If you have a language that supports Regular Expressions, that could give you much better validation.
LOL. Brendan..I have NO idea what you just said!
If your server has PHP, heres how you can validate a phone number (I just made this real quick, and im bad at regexps, so its probably not perfect. It doesn't like international numbers, or people that put their area code in parenthesis).

PHP:
if(ereg("^([2-9]{3})(-?)([0-9]{3})(-?)([0-9]{4})$", $phone))
{echo("$phone is a valid phone number");}
else {"$phone is not a valid phone number");}
It will validate a number that
-Starts with 3 digits, 2-9
-Optionally a - between area code + first 3 digits of number
-3 Digits 0-9
-Optionally a - between first 3 digits and last 4 digits of number
-4 digits, 0-9
See less See more
Thanks brendan but you didn't have to go to all that work. I don't know a thing about PHP other then yes, the server does support it. I've been trying to learn a bit about it on the internet but I can't really find anything for beginners who know NOTHING. Oh well. I will keep this info though for the future. Thanks.
May I ask what you are using to process the form?
If your using another language (Perl, ASP, whatever), I'm sure you can validate a phone number in that language.
If your just using a mailto: command or something, I HIGHLY reccommend you switch to PHP. Its much more powerful & reliable. You don't even need to learn the language to process a form, there are ready-made scripts for this.
I'm just using HTML. I'm just using "insert form" in dreamweaver, then using FormMail from the server. It's just a small little form with a few fields but the client whats to make sure the use puts in an actual phone number and email address. I will switch to PHP when I figure it out. Thanks for the help.
Heres a cut n paste PHP FormMail script.
http://webdevfaqs.com/php.php#mailer
With a few modifications, it can validate the form fields.
Great! Thanks so much!
1 - 11 of 11 Posts
Status
Not open for further replies.
Top