email validation

G

Guest

Hi,

I have to validate TextBox.
I have a customValidate control for textbox. I have following function for
customValidate control.

but if I type in textbox bobby
It throws exception that index shouldnot be less than zero
but if I type (e-mail address removed)(bobby gill). It works. I want that if I if I
type bobby the validation control gives me error that it is not valid


public void ValidateUserID(object sender, ServerValidateEventArgs args)
{
CustomValidator valDate = sender as CustomValidator;
args.IsValid = true;
string str = args.Value;
//string str = this.txtAddUsers.Text;
string subStr1 = str.Substring(0, str.IndexOf("( "));
bool isValidEmail = IsValidEmail(subStr1);


if (isValidEmail == true)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
valDate.ErrorMessage = "Email is Invalid!";
return;
}

}
public static bool IsValidEmail(string EmailAddress)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(EmailAddress,
@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
 
G

Guest

"bobby" doesn't have '(' in it and so IndexOf returns -1 which cannot be used
in Substring operation. So, assign the return value of IndexOf to a variable,
compare it and then use it for subsequent processing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top