Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Check for repeated characters

Check for repeated characters

Scheduled Pinned Locked Moved ASP.NET
toolsjavascriptcomtestinghelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    padmanabhan N
    wrote on last edited by
    #1

    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[^]

    N J M 3 Replies Last reply
    0
    • P padmanabhan N

      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[^]

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P padmanabhan N

        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[^]

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        padmanabhan N wrote:

        But is there any quick process for this?

        if string.contains("-") FileName = FileName.Replace("#", "_")

        N 1 Reply Last reply
        0
        • J jkirkerx

          padmanabhan N wrote:

          But is there any quick process for this?

          if string.contains("-") FileName = FileName.Replace("#", "_")

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • P padmanabhan N

            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[^]

            M Offline
            M Offline
            markovl
            wrote on last edited by
            #5

            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 the System.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 the IsMatch method from the Regex 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 its test 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

            1 Reply Last reply
            0
            • N Not Active

              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.

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups