Web Analytics Made Easy -
StatCounter Regular expressions - CodingForum

Announcement

Collapse
No announcement yet.

Regular expressions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Regular expressions

    Hi,

    I feel stupid that I can't find a solution to this. I want that in a input box, you only can enter letters, numbers and a space. I know how to do it in PHP, but I can't seem to make it work in javascript.

    I've tried some things, but it still doesn't work, here is one of my attempts:

    Code:
    ge.value.match(/^([a-z0-9])\s$/)
    Anyone knows how to do it right?

  • #2
    For the beginning, take a look on the RegExp for javascript:
    http://lawrence.ecorp.net/inet/samples/regexp-intro.php
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Originally posted by Kor View Post
      For the beginning, take a look on the RegExp for javascript:
      http://lawrence.ecorp.net/inet/samples/regexp-intro.php
      I know I could use the (\s Single white space) to accept a space, but the incorporartion with the rest doesn't work, and I wonder how I do it the right way?

      Comment


      • #4
        You can prevent the user from entering invalid characters by doing something like this:
        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Test</title>
        <script type="text/javascript">
        // <![CDATA[
        
        function allow(e,input_element)
        	{
        	e = e || window.event;
        	var key = String.fromCharCode(e.which || e.keyCode);
        	return /[a-zA-Z0-9 ]/.test(key);
        	}
        
        // ]]>
        </script>
        </head>
        <body>
        
        <form onsubmit="return false">
        	<input type="text" name="my_input" onkeypress="return allow(event,this)" /><br />
        	<input type="submit" value="submit form" />
        </form>
        
        </body>
        </html>
        Of course that is a bit intrusive and not bulletproof (because the user could still copy and paste invalid characters). Another method is to validate on the input's blur or on the form submit, and letting the user know what he did wrong. You can build off something like this:
        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Test</title>
        <script type="text/javascript">
        // <![CDATA[
        
        function onFormSubmit(form_element)
        	{
        	var non_alpha_numeric = new RegExp("[^a-zA-Z0-9 ]");
        	if (non_alpha_numeric.test(form_element.my_input.value)) return false;
        	}
        
        // ]]>
        </script>
        </head>
        <body>
        
        <form onsubmit="return onFormSubmit(this)">
        	<input type="text" name="my_input" /><br />
        	<input type="submit" value="submit form" />
        </form>
        
        </body>
        </html>
        Last edited by itsallkizza; Apr 10, 2009, 06:53 PM.
        Feel free to e-mail me if I forget to respond ;)
        [email protected]

        Comment


        • #5
          I would have used the replace() method and I would have called the function onkeyup and onblur:
          Code:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
          <html>
          <head>
          <title>untitled</title>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <meta http-equiv="Content-Style-Type" content="text/css">
          <meta http-equiv="Content-Script-Type" content="text/javascript">
          <script type="text/javascript">
          function valid(f) {
          f.value=f.value.replace(/[^A-z-0-9-\s]/g,'');
          } 
          </script>
          </head>
          <body>
          <form action="">
          <input type="text" onkeyup="valid(this)" onblur="valid(this)">
          </form>
          </body>
          </html>
          Last edited by Kor; Apr 11, 2009, 12:57 AM.
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment


          • #6
            A big goof in there!
            Code:
            f.value=f.value.replace(/[^A-z-0-9-\s]/g,'')
            That will *allow* all the characters between upper case Z and lower case as well as the dash (minus) sign.

            That is, it would allow
            [ (left square bracket)
            \ (backslash)
            ] (right square bracket)
            ^ (caret, shift-6)
            _ (underline)
            ` (back tick...shares key with ~ usually)
            - (dash or minus sign)
            in addition to letters and digits and space.

            And it's a somewhat fine point, but \s does *NOT* mean "space".

            It means "white space".

            And that *INCLUDES* newlines (both \r and \n) and tabs (\t). You can't get \r or \n into a text field, but a user *can* use copy/paste to put a tab character into a text field!!! Unlikely but...

            So the SAFE regexp is:
            Code:
            function valid(f) {
                f.value=f.value.replace(/[^A-Z0-9 ]/ig,'');
            }
            and that *is* a space character after the 9.
            Be yourself. No one else is as qualified.

            Comment


            • #7
              ok, good point.
              KOR
              Offshore programming
              -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

              Comment

              Working...
              X