Posted by : Sudhir Chekuri Monday, 7 March 2016

Text given by user as input is validated before saving it in database tables on client side using Regular Expressions.

Regular Expressions are the patterns which are used to validate the text based on some conditions.
Regex is a class under System.Text.RegularExpressions namespace which is used to validate text based on the given pattern using IsMatch method.
IsMatch method will take string as input to validate with pattern and return true if the text is matched with the pattern or returns false if the text is not matched with the pattern.

Writing patterns

^ - start

$ - end

[a-z] – lower case letters are allowed

[A-Z] – Upper case letters are allowed

[0-9] – Numbers are allowed

[a-zA-Z] – both lower and upper case letters are allowed

[a-zA-Z0-9] – input can be lower case/upper case or numbers

{10} – only 10 characters is valid

{6,10} – 6 to 10 characters are valid

Examples:
10 digit phone number - ^[0-9]{10}$
6 to 15 characters are valid which may contain lower/upper letters -
^[a-zA-Z]{6,15}$

Example to display popup with error message if text is not a valid

//Validate text with given pattern and return true for valid and false for invalid text
        public bool TextValidate(string Text, string Pattern)
        {
            //Create regular expression object with specific pattern
            var regex = new Regex(Pattern);
            //Check if text matched with pattern
            if (regex.IsMatch(Text))
            {
                return true;
            }//if text not matched with pattern
            else { return false; }
       
        }
        private void BtnRegister_Click(object sender, EventArgs e)
        {
           
            //intialize errormsg as empty
            string errormsg = string.Empty;
            //Username - 6 to 15 chars(Upper/Lower) Ex: SudhirChekuri
            if (!TextValidate(TxtUsername.Text, "^[a-zA-Z]{6,15}$"))
            {
                errormsg = "Invalid Username\nShould be between 6 to 15 letters";
            }
                //Password and cnfrm pwd match
                 else if(TxtPassword.Text!=TxtCnfmPwd.Text)
            {
            errormsg = "Password and Confirm Password Mismatch";
            }//Email Id - ex: sudhir@gmail.com
            else if (!TextValidate(TxtEmailId.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
            {
                errormsg = "Invalid Email Id";
            }
            //phone number - 10 digits Ex:9985333333
            else if (!TextValidate(TxtPhoneNo.Text, "^[0-9]{10}$"))
            {
                errormsg = "Invalid Phone Number\nEnter 10 digit number";
            }
          
            if (errormsg != string.Empty)
            {
                MessageBox.Show(errormsg);
            }

}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -