nice nice , but how do you put this in a cshtml page with a Razor syntax , that is what i dont get , cause you are showing me a webform not a cshtml page.
I have an email adres that uses the TLD .name, what I hate is that in a lot of sites I can't use my valid email address because these sites complaint about my valid email address being invalid.
Do you get the hint?
Give a man a fish and you will feed him for a day. Teach a man to fish and you will feed him for a lifetime.
That is the best way. Web Pages is still in its early stages. There's a lot of room for additional helpers, although having a baked in email address validator could be problematic. As gabriel kind of alluded to, specificity in email address validation is
a problem. Some people would be happy with something quite loose, whereas others may want something a lot more restricted.
I understand , but as i know nothing about ASP , c# , MVC.ASP or as webpages works , i had to ask the question.
after searching a lot today ( i must finish a webmatrix project by the weekend ) here is what i managed :
I created a folder APP_CODE where i put my new class , MyValidator.cs
here is the class
using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
///
/// My class validator
///
public class MyValidator{
public static bool IsEmailAdress(string sEmail){
if(sEmail!=""){
var sRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return sRegex.IsMatch(sEmail) ?true:false ;
}else{
return false ;
}
}
}
and in the cshml file :
@{
var emails = new String[]{"ante.marcus@gmail.com","dude@bobland"};
//
}
Testing wether it is an email or not
@foreach(var email in emails){
if(MyValidator.IsEmailAdress(email)){
@email is an email adress
}else{
@email is not an email adress
}
}
the goal is just an indication for the user so he checks what he enters in a form.
It shows noobs how to create simple functions too , as i did not find if i could define functions within Razor.
camus
Member
209 Points
66 Posts
Email validation
Sep 24, 2010 07:55 AM|LINK
Hi , i was wondering what was the simplest way to validate a form field. i come from php , i know regexp but i see there is a validation class.
How can i use it ?
btw , webmatrix rocks for simple professional projects.
Email validation regexp
SimpleBlogCms powered by Razor and WebMatrix
chandrasheka...
Star
10258 Points
1760 Posts
Re: Email validation
Sep 24, 2010 08:08 AM|LINK
You can use the RegularExpressionValidator control for validating the email entered in the text box.
Here refer the following link:
http://www.java2s.com/Code/ASP/Validation-by-Function/aspRegularExpressionValidatorforemailaddressC.htm
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
dk_4_asp
Participant
904 Points
361 Posts
Re: Email validation
Sep 24, 2010 08:12 AM|LINK
you can use regularexpressionvalidator..............and there you can select type of regular expression as email address.
Mark as answer if it helps you.
DK
India
camus
Member
209 Points
66 Posts
Re: Email validation
Sep 24, 2010 08:29 AM|LINK
nice nice , but how do you put this in a cshtml page with a Razor syntax , that is what i dont get , cause you are showing me a webform not a cshtml page.
SimpleBlogCms powered by Razor and WebMatrix
dk_4_asp
Participant
904 Points
361 Posts
Re: Email validation
Sep 24, 2010 08:36 AM|LINK
you can copy that regular expression and use it.
DK
India
chandrasheka...
Star
10258 Points
1760 Posts
Re: Email validation
Sep 24, 2010 08:48 AM|LINK
Check the following javascript function which takes email as parameter and validates using the regular expression.
function email_validate(email) { var regMail = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/; if(regMail.test(email) == false) { alert("Email address is not valid yet."); return false; } else { return true; } }Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
gabriel.loza...
Contributor
3583 Points
800 Posts
Re: Email validation
Sep 24, 2010 09:05 AM|LINK
I have an email adres that uses the TLD .name, what I hate is that in a lot of sites I can't use my valid email address because these sites complaint about my valid email address being invalid.
Do you get the hint?
camus
Member
209 Points
66 Posts
Re: Email validation
Sep 24, 2010 08:27 PM|LINK
Ok , it seems there is not built in object for email validation , i'm gona built a static method like
that returns a boolean to validate emails
thanks
Email validation regexp
SimpleBlogCms powered by Razor and WebMatrix
Mikesdotnett...
All-Star
155599 Points
19982 Posts
Moderator
MVP
Re: Email validation
Sep 24, 2010 08:43 PM|LINK
That is the best way. Web Pages is still in its early stages. There's a lot of room for additional helpers, although having a baked in email address validator could be problematic. As gabriel kind of alluded to, specificity in email address validation is a problem. Some people would be happy with something quite loose, whereas others may want something a lot more restricted.
Web Pages CMS | My Site | Twitter
camus
Member
209 Points
66 Posts
Re: Email validation
Sep 24, 2010 09:32 PM|LINK
I understand , but as i know nothing about ASP , c# , MVC.ASP or as webpages works , i had to ask the question.
after searching a lot today ( i must finish a webmatrix project by the weekend ) here is what i managed :
I created a folder APP_CODE where i put my new class , MyValidator.cs
here is the class
using System; using System.Collections.Generic; using System.Web; using System.Text.RegularExpressions; /// /// My class validator /// public class MyValidator{ public static bool IsEmailAdress(string sEmail){ if(sEmail!=""){ var sRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); return sRegex.IsMatch(sEmail) ?true:false ; }else{ return false ; } } }and in the cshml file :
@{ var emails = new String[]{"ante.marcus@gmail.com","dude@bobland"}; // }Testing wether it is an email or not
@foreach(var email in emails){
if(MyValidator.IsEmailAdress(email)){
@email is an email adress
}else{@email is not an email adress
} }the goal is just an indication for the user so he checks what he enters in a form.
It shows noobs how to create simple functions too , as i did not find if i could define functions within Razor.
Regards
Email validation regexp
SimpleBlogCms powered by Razor and WebMatrix