Special Symbol

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danasegarane
    New Member
    • Mar 2007
    • 32

    Special Symbol

    Hi all,
    I want to check any - symbol is present in the textbox.I am trying with this code.But this is not working.Can some one help.The condition is that it should contain only one hiphen symbol
    Code:
    function isHiphenpresent(value)
    	{
    		var j, k;
    		for(j=0;j<value.length;j++) 
    		{
    			k = value.substr(j,1);
    			if((k=='-'))
    				;
    			else
    				return false;
    		}
    		return true;
    	}
    Thanks in Advance
    Dana
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Consider using regular expressions instead.

    Comment

    • danasegarane
      New Member
      • Mar 2007
      • 32

      #3
      Thanks for your reply.
      Can you give one example ?

      Dana

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by danasegarane
        Thanks for your reply.
        Can you give one example ?

        Dana
        One possible example is:
        [CODE=javascript]/^[^-]*(-){1}[^-]*$/[/CODE] If you need an explanation on how tis works, let me know.

        See how to use regular expressions in Javascript here.

        Comment

        • danasegarane
          New Member
          • Mar 2007
          • 32

          #5
          It will be better ,It you give the example with some explanation


          Dana

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by acoder
            One possible example is:
            [CODE=javascript]/^[^-]*(-){1}[^-]*$/[/CODE]
            The /'s denote a regular expression in Javascript.

            The ^ at the beginning matches the beginning of the string. The ^ inside the square brackets is a negation, so [^-] matches everything besides a hyphen. The star means "match 0 or more times".

            Then we match the actual hyphen, but only once ({1}). Then, we again match anything besides a hyphen 0 or more times. Finally, we match the end of the string with $.

            Read the tutorial link for more information.

            Comment

            • danasegarane
              New Member
              • Mar 2007
              • 32

              #7
              Thanks Acoder,
              I wll read the tutorial and come back. :)

              Dan

              Comment

              Working...