What I am trying to do is to create a contact form on my project and I want to use validation for boxes. In my contact form, if a user puts the info it would work. if not, it wouldn't work. but however I want some "value" elements appear in the text boxes.
so the thing is coming here. I don't know how to do. When I put the "value" element in the textbox, the "if else" element detects the "value" and understands that something is written as info. so, here I need another "if else" condition I think. please see
the code below to see what I am saying;
@{
if(IsPost)
{
var valer = false;
var nme = Request["name"];
if(nme.IsEmpty())
{
valer = true;
@: Please enter your name
}
if(valer == false)
{
<div id="contact-form">Dear @nme, thanks for your email.</div>
}
}
else
{
<form id="contact-form" method="post" enctype="multipart/form-data">
<fieldset>
<div>
<label for="name"> <input name="name" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" /></label>
</div>
</fieldset>
</form>
}
}
it is difficult to explain. :) I use "value" in "input". however, the "if else" would return with an ERROR message if it was empty. But "if else" understands the "value" as user entered info. dont know how to explain better :)
actually there I want to insert the second condition. The first is IsEmpty...and the second must be what I dont know. It means, If the equal is the "value" from input, how can I show it in if else statement ?
Dear GmGregori thanks a lot for your effort and help. I am done with the problem.
Using webmatrix is kinda mix of languages. That is why it seems complicated and I sometime lose myself inside the code. However asp website
here is making thinghs easier for users.
proy
Member
36 Points
90 Posts
How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 05:27 AM|LINK
Hello everyone,
I need your help about validation in form work.
What I am trying to do is to create a contact form on my project and I want to use validation for boxes. In my contact form, if a user puts the info it would work. if not, it wouldn't work. but however I want some "value" elements appear in the text boxes. so the thing is coming here. I don't know how to do. When I put the "value" element in the textbox, the "if else" element detects the "value" and understands that something is written as info. so, here I need another "if else" condition I think. please see the code below to see what I am saying;
@{ if(IsPost) { var valer = false; var nme = Request["name"]; if(nme.IsEmpty()) { valer = true; @: Please enter your name } if(valer == false) { <div id="contact-form">Dear @nme, thanks for your email.</div> } } else { <form id="contact-form" method="post" enctype="multipart/form-data"> <fieldset> <div> <label for="name"> <input name="name" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" /></label> </div> </fieldset> </form> } }Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 06:53 AM|LINK
You can read this tutorial: http://www.asp.net/web-pages/tutorials/working-with-pages/4-working-with-forms or you can look at the Starter Site o Bakery templates to see how to use the Validation helper:
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
proy
Member
36 Points
90 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 07:42 AM|LINK
it is difficult to explain. :) I use "value" in "input". however, the "if else" would return with an ERROR message if it was empty. But "if else" understands the "value" as user entered info. dont know how to explain better :)
proy
Member
36 Points
90 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 07:43 AM|LINK
I checked the link but no explanation for my question
thanks anyway for trying help
GmGregori
Contributor
5470 Points
737 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 08:11 AM|LINK
Maybe I simplify to much, but why don't you test for nme == "Name:"?
Try to replace
with
proy
Member
36 Points
90 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 08:33 AM|LINK
still no working.
actually there I want to insert the second condition. The first is IsEmpty...and the second must be what I dont know. It means, If the equal is the "value" from input, how can I show it in if else statement ?
GmGregori
Contributor
5470 Points
737 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 12:50 PM|LINK
If I have understood your goal, this should accomplish it:
@{ var valer = false; var error = "Please enter your name"; var nme = ""; if(IsPost) { nme = Request["name"]; if (nme.IsEmpty() || nme == "Name:") { valer = true; } } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form id="contact-form" method="post" enctype="multipart/form-data"> <fieldset> <p> <label for="name">Name:</label> <input type="text" name="name" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value=='Name:') this.value=''" /> @if (valer) { <label for="name" class="validation-error">@error</label> } </p> <p> <input type="submit" value="Register" title="Register" /> </p> </fieldset> </form> <div> @if (IsPost && !valer) { @:Dear @nme, thanks for your email. } </div> </body> </html>proy
Member
36 Points
90 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 05:18 PM|LINK
thanks a lot gmgregori.
that is exactly what I have been trying to do. thanks alot.
however, my code a little bir large and styling different. would you show me below in my code only for name and I would try the last.
Thanks in advance.
@using System.Text.RegularExpressions @{ if(IsPost) { var valer = false; var nam = Request["name"]; var ema = Request["email"]; var pho = Request["phone"]; var msj = Request["message"]; if(nam.IsEmpty()) { valer = true; @: <div id="contact-form"> Please enter your name</div> } if(ema.IsEmpty()) { @: <div id="contact-form"> Please enter an email address.</div> } else { var isValidEmail = true; var pattern = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"; var regExp = new Regex (pattern); isValidEmail = regExp.IsMatch(ema); if(isValidEmail == false) { @: <div id="contact-form"> Please enter a valid email address.</div> } } if(msj.IsEmpty()) { @: <div id="contact-form"> Please type your message.</div> } if(valer == false) { <div id="contact-form">Dear @nam thanks for the message.</div> } } else { <form id="contact-form" method="post" enctype="multipart/form-data"> <fieldset> <div> <label for="name"> <input name="name" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" /></label> </div> <div> <label for="email"><input name="email" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''" /></label> </div> <div> <label for="phone"><input name="phone" value="Phone:" onBlur="if(this.value=='') this.value='Phone:'" onFocus="if(this.value =='Phone:' ) this.value=''" /></label> </div> <div> <label class="message" for="message"><textarea onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''">Message:</textarea></label> </div> <div class="buttons"> <a class="button2" href="#" onClick="document.getElementById('contact-form').reset()">clear</a> <a class="button2" href="#" onClick="document.getElementById('contact-form').submit()">send</a> </div> </fieldset> </form> } }GmGregori
Contributor
5470 Points
737 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 14, 2012 08:42 PM|LINK
To implement the name control you must simply replace
with
If you want to modify more in depth your code I think that is a better solution to rewrite it.
I'm not the right person for teaching a programming style, but I believe that you are mixing to much code with html.
Lastly, remember to add
to the tag
proy
Member
36 Points
90 Posts
Re: How to do a second Condition for Validation in If Else element ???
Feb 15, 2012 04:42 AM|LINK
Dear GmGregori thanks a lot for your effort and help. I am done with the problem.
Using webmatrix is kinda mix of languages. That is why it seems complicated and I sometime lose myself inside the code. However asp website
here is making thinghs easier for users.
thanks a again.