Regular expression for validating email address in asp net

26 Jan

Remembering that “\d” means “any digit” and “\D” means “any non-digit”, we can do this to restrict the input to, say, no more than seven digits: [Regex]:: Is Match(“-123456.78”, “^(\D?

If we are going to do “logical and” patterns as opposed to simply “and then” patterns, we need a way of applying multiple sub-patterns to the same input. NET regular expressions support the obscurely named, but very powerful, “lookahead” feature which allows us to do just that. “)” and here’s how we can use it to implement the requirement “up to seven digits AND a valid decimal number” by combining our two existing patterns: [Regex]:: Is Match(“-12345.6.7”, “^(? Our final input validation requirement was for address lines, to exclude any that used a PO Box instead of a real (residential) address. This can be done by setting a mode at the start of the expression that will apply to everything that follows it. In order to do this we’ve covered writing straight-forward patterns using literals, special characters and character classes, and applying them to the whole input using “^” … We’ve also seen how to combine simple patterns to implement logical OR, AND and NOT rules.

Using this feature we can, from our current position in the input string, test a pattern over the rest of the string (all the way to the end if necessary), then resume testing from where we were. As usual, let’s revisit the friendly spec: Let’s first implement the rule that the string must be alphanumeric. We can specify “case insensitive mode” with the notation “(? Using Regular expressions to validate input in ASP. tutorial_id=46 Using Regular expressions to in SQL Server 2005: expression options in the .

We need to escape it with the “\” prefix to make it a literal: [Regex]:: Is Match(“,”, “^\.$”)False The next step is to use the braces modifier to specify that we want one to two digits following the decimal point – we can put the minimum and maximum number of matches (in our case 1 and 2, which we’ll test with zero to three) inside the “” curly brackets: [Regex]:: Is Match(“123”, “^-? “]” expressions, to accept alternative single characters, but the requirement for UK Bank Sort Codes requires us to accept input strings that fall into one of two different patterns.

[0-9] \.[0-9]$”)False Aha, we should still be accepting numbers with no decimal places, but we’re not. Let’s take another look at the requirement: [Regex]:: Is Match(“12-34-56”, “^\d\d-\d\d-\d\d$”)True We can match one pattern or the other using the “|” (or) operator.

Francis Norton shows how to use regular expressions to fulfil some real world data validation requirements, demonstrating techniques ranging from simple number format checks, to complex string validation that requires use of regex's powerful "lookahead" feature. NET regular expressions to enforce the kind of logically complex input validation requirements that we sometimes confront in real specifications.