Check for repeated characters
-
Experts, I am working on a Email validation. My requirement is i have to check for the email does not contain hyphen(-) repeated continuously for 2 times. Example: a-b-c@d.com – valid a—bc@d.com – not valid since hyphen(-) occurs continuously for 2 times I have to validation both in code and also using javascript. I have written a method like comparing the charactes and incrementing the local variable. But is there any quick process for this? Your help would be highly appreciated.
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]
-
Experts, I am working on a Email validation. My requirement is i have to check for the email does not contain hyphen(-) repeated continuously for 2 times. Example: a-b-c@d.com – valid a—bc@d.com – not valid since hyphen(-) occurs continuously for 2 times I have to validation both in code and also using javascript. I have written a method like comparing the charactes and incrementing the local variable. But is there any quick process for this? Your help would be highly appreciated.
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]
Use a Regular Expression. You can start here: http://www.regular-expressions.info/regexbuddy/email.html[^]
Failure is not an option; it's the default selection.
-
Experts, I am working on a Email validation. My requirement is i have to check for the email does not contain hyphen(-) repeated continuously for 2 times. Example: a-b-c@d.com – valid a—bc@d.com – not valid since hyphen(-) occurs continuously for 2 times I have to validation both in code and also using javascript. I have written a method like comparing the charactes and incrementing the local variable. But is there any quick process for this? Your help would be highly appreciated.
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]
-
padmanabhan N wrote:
But is there any quick process for this?
if string.contains("-") FileName = FileName.Replace("#", "_")
Did you even bother to read the question? 1) email address not filename 2) the OP is looking for repeating characters, not every instance 3) the characters are not being replaced
Failure is not an option; it's the default selection.
-
Experts, I am working on a Email validation. My requirement is i have to check for the email does not contain hyphen(-) repeated continuously for 2 times. Example: a-b-c@d.com – valid a—bc@d.com – not valid since hyphen(-) occurs continuously for 2 times I have to validation both in code and also using javascript. I have written a method like comparing the charactes and incrementing the local variable. But is there any quick process for this? Your help would be highly appreciated.
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]
Hi, As Mark noted, it may be easiest to solve this by using Regular Expressions. On the server side you should use the
Regex
class found in theSystem.Text.RegularExpressions
namespace. A simple pattern to match exactly two repeated hyphens looks like"-{2}"
. If you want to match 2 and more consecutive hyphens, the pattern may be modified to"-{2,}"
. Then you can use theIsMatch
method from theRegex
class to test a string (In your case the email) like this:Regex regex = new Regex("-{2}");
bool isValid = !regex.IsMatch(emailString);On the client side is even easier. You can use the same pattern with the
RegExp
JavaScript object. Then, call itstest
method. Something like this:var regex = new RegExp('-{2}');
var isValid = !regex.test(emailString);// Even easier way is to use the String.search method and the RegExp shorthand:
var isValid = emailString.search(/-{2}/) == -1;I hope this is helpful ;)
2A
-
Did you even bother to read the question? 1) email address not filename 2) the OP is looking for repeating characters, not every instance 3) the characters are not being replaced
Failure is not an option; it's the default selection.
I read the question, but I broke the rule of providing a clear comprehensive answer for email address. The replace was just a quick way to to correct the double hyphen on the server side. Just food for thought. string.replace("--", "-") on the JQuery/Javascript side, I would just use regex, I haven't tested the regex for double hyphen, but I should today.
var re_EmailAddress = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\w+)*\\.\\w+([-.]\\w+)*")
var txt_EmailAddress_Validate = $('[id*="_txt_CreateNewAccount_Email_Field"]').val().trim();
var match_EmailAddress = re_EmailAddress.exec(txt_EmailAddress_Validate);
if (match_EmailAddress == null) {
// Invalid email address
}
else {
// valid email address
}I suppose there is a way to step through the email address in Javascript one char at a time, and get the position of the first hyphen, and check for a hyphen after the first occurrence. But I'm not going to experiement with that right now.