email validation

Discussion in 'ASP .Net' started by =?Utf-8?B?Ym9iYnk=?=, Sep 14, 2007.

  1. 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 (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})(\]?)$");
     
    =?Utf-8?B?Ym9iYnk=?=, Sep 14, 2007
    #1
    1. Advertising

  2. "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.

    "bobby" wrote:

    > 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 (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})(\]?)$");
    >
     
    =?Utf-8?B?U2l2YSBN?=, Sep 14, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    3,412
    Peter
    Jul 1, 2003
  2. John Silver
    Replies:
    0
    Views:
    627
    John Silver
    Feb 4, 2005
  3. Tee

    Email ErrorPage to email

    Tee, Jun 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    392
  4. Replies:
    3
    Views:
    561
    AdamM
    Jul 27, 2004
  5. Chuck Amadi
    Replies:
    2
    Views:
    416
    chuck amadi
    Jun 6, 2004
Loading...

Share This Page