Click to See Complete Forum and Search --> : Form Validation


myself1
11-20-2006, 04:03 PM
Hi,
Further to my Contact Form which Riz ;) sorted out and works wonders!! I could do with knowing how to add form validation to this form now:

<form name="form1" method="post" action="mail.asp">
Name:
<input name="name" type="text" id="name"><br>
City: <input name="city" type="text" id="city"><br>
Email: <input name="email" type="text" id="email"><br>
Enquiry:
<select name="enquiry" id="enquiry">
<option value="Advertisement" selected>Advertisement</option>
<option value="Sponsorship">Sponsorship</option>
<option value="Other">Other</option>
</select>
<input type="hidden" name="emailFrom" value="tmp@googlemail.com">
<input type="hidden" name="emailTo" value="admin@themiddlepath.co.uk">
<input type="hidden" name="emailSubject" value="Online Contact Form">
<input type="hidden" name="thanksPage" value="thanks.htm">
</form>

I only need to add validation to the name and email fields. Any advice appreciated.

nbcrockett
11-21-2006, 06:32 AM
I suggest you use Javascript not ASP for the form validation.

veljkoz
11-23-2006, 02:30 PM
You can add a required field validator:
<asp:requiredfieldvalidator id="valName" Runat="server" ErrorMessage="Name is missing" Display="Static" ControlToValidate="name"></asp:requiredfieldvalidator>

The ASP should recognize whether the client browser supports javascript and it will render the above validator as a javascript code that checks the input before the form is submitted.
However, in submit function (back in asp) you should check if the validation passed by examining the .IsValid property of the valName validator.

I hope that's the validation you need...

so_is_this
11-23-2006, 02:55 PM
I suggest you use Javascript not ASP for the form validation.
If you want to add JavaScript form validation, that is fine. But, do not eliminate ASP form validation or you'll leave yourself wide open to spammers (among other malcontents) -- whom use a client proxy to bypass the use of a browser (and JavaScript) altogether.

mrizwan
11-24-2006, 01:57 AM
Change <form name="form1" method="post" action="mail.asp"> to
<form name="form1" method="post" action="mail.asp" onsubmit="return validate();">

And put following code between your page's head tag.


<script language="javascript">
function validate() {
with(document.form1) {
if(name.value=='') {alert('Fill name field'); name.focus(); return false;}
if(email.value=='') {alert('Fill email field'); email.focus(); return false;}
if(city.value=='') {alert('Fill city field'); city.focus(); return false;}
return true;
}
}
</script>


This should do a simple js validation for you.
Further to my Contact Form which iz sorted out and works wonders!!
and it was Riz not iz :)

nbcrockett
11-24-2006, 08:49 PM
How do you use asp for validation within the page?

mrizwan
11-24-2006, 09:38 PM
How do you use asp for validation within the page?
AP validation will not be within the page but you will have to do the validation on the server side where the form is submitted.
If form has errors redirect user back to the form page otherwise process the submitted form.

nbcrockett
11-24-2006, 10:19 PM
That's what I thought. Thanks.

myself1
01-22-2007, 01:59 AM
Hi,
What do I need to alter so that users have to put in the @ sign? I have the following script:


<script language="javascript">
function validate() {
with(document.form1) {
if(name.value=='') {alert('Fill name field'); name.focus(); return false;}
if(email.value=='') {alert('Fill email field'); email.focus(); return false;}
if(city.value=='') {alert('Fill city field'); city.focus(); return false;}
return true;
}
}
</script>

Thanks!

nbcrockett
01-22-2007, 07:27 AM
This is what I use for Javascript field validation. I highlighted in red the email part.

function CRLF (){
return String.fromCharCode(10) + String.fromCharCode(13);
}
function Validate(){
var x=document.forms.Refer
var strAlert=""
var submitOK="True"
//Field Validation
if(x.txtFirstName.value==""){
strAlert=strAlert + "Please enter your friend's first name." + CRLF()
submitOK="False"
}
if(x.txtLastName.value==""){
strAlert=strAlert + "Please enter your friend's last name." + CRLF()
submitOK="False"
}

var strEmailAddress=x.txtEmailAddress.value
if(x.txtEmailAddress.value==""){
strAlert=strAlert + "Please enter your friend's email address." + CRLF()
submitOK="False"
}
else if(x.txtEmailAddress.value!=""){
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)")
var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$")
if(!r1.test(strEmailAddress) && r2.test(strEmailAddress)==false){
strAlert=strAlert + "Please enter a valid email address." + CRLF()
submitOK="False"
}
}

if(x.txtCity.value==""){
strAlert=strAlert + "Please enter your friend's city." + CRLF()
submitOK="False"
}
//Alert Message
if(submitOK=="True"){
var message=confirm("Are you sure that you entered all of the information correctly?")
if(message==true){
return true
}
else{
return false
}
}
else{
alert(strAlert)
return false
}
}