Regular expression , for only one word

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Abou Jaoude

    #1

    Regular expression , for only one word



    hi,
    I need to verify that the user enter only one word in a textbox.
    So i thought to add a regularexpressi on validator and disallow the user
    to enter a space between the letters he wrote.
    how can i do that ?

    thx


    *** Sent via Developersdex http://www.developersdex.com ***
  • Dietmar Meier

    #2
    Re: Regular expression , for only one word

    Joe Abou Jaoude wrote:
    [color=blue]
    > I need to verify that the user enter only one word in a textbox.
    > So i thought to add a regularexpressi on [...][/color]

    <input ... onchange="if (yourRegExp.tes t(value)) alert('Invalid input')">
    or
    <input ... onchange="if (!yourRegExp.te st(value)) alert('Invalid input')">

    What you replace "yourRegExp " with depends on what you consider to be a
    word. Take a look at this table for some possible values:

    <body>
    <script type="text/javascript">
    var aRegExps = [
    /^\S+$/,
    /^\w+$/,
    /\s/,
    /\W/,
    /^[a-z]+$/i
    ],
    aStrings = [
    "Foobar",
    "Foo Bar",
    "Foo_Bar",
    "Foo.Bar",
    "Foo-Bar",
    " Foobar",
    "Foobar ",
    "Foo1Bar",
    "\u0641\u0648\u 0628\u0627\u063 1"
    ],
    sOutput = "<table border='1'><the ad><tr><th><\/th>",
    i,
    j;
    for (i=0; i<aRegExps.leng th; i++) {
    sOutput += "<th>" + aRegExps[i].source + "<\/th>";
    }
    sOutput += "<\/tr><\/thead><tbody>";
    for (j=0; j<aStrings.leng th; j++) {
    sOutput += "<tr><td>&quot; " + aStrings[j] + "&quot;<\/td>";
    for (i=0; i<aRegExps.leng th; i++) {
    sOutput += "<td>" + aRegExps[i].test(aStrings[j]) + "<\/td>";
    }
    sOutput += "<\/tr>";
    }
    sOutput +=" <\/tbody><\/table>";
    document.write( sOutput);
    </script>
    </body>

    ciao, dhgm

    Comment

    • RobB

      #3
      Re: Regular expression , for only one word

      Joe Abou Jaoude wrote:[color=blue]
      > hi,
      > I need to verify that the user enter only one word in a textbox.
      > So i thought to add a regularexpressi on validator and disallow the[/color]
      user[color=blue]
      > to enter a space between the letters he wrote.
      > how can i do that ?
      >
      > thx
      >
      >
      > *** Sent via Developersdex http://www.developersdex.com ***[/color]

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <style type="text/css">

      input {
      font: 16px tahoma;
      }

      </style>
      </head>
      <body>
      <form>
      <input
      type="text"
      name="t1" value=""
      onkeydown="this .prev_value=thi s.value"
      onkeyup="var
      v=this.value;if (v!=this.prev_v alue)this.value =v.replace(/\s+/,'')" />
      one word only !
      </form>
      </body>
      </html>

      Comment

      • RobB

        #4
        Re: Regular expression , for only one word

        Joe Abou Jaoude wrote:[color=blue]
        > hi,
        > I need to verify that the user enter only one word in a textbox.
        > So i thought to add a regularexpressi on validator and disallow the[/color]
        user[color=blue]
        > to enter a space between the letters he wrote.
        > how can i do that ?
        >
        > thx
        >
        >
        > *** Sent via Developersdex http://www.developersdex.com ***[/color]

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <style type="text/css">

        input {
        font: 16px tahoma;
        }

        </style>
        </head>
        <body>
        <form>
        <input
        type="text"
        name="t1" value=""
        onkeydown="this .prev_value=thi s.value"
        onkeyup="var
        v=this.value;if (v!=this.prev_v alue)this.value =v.replace(/\s+/,'')" />
        one word only !
        </form>
        </body>
        </html>

        Comment

        • otto

          #5
          Re: Regular expression , for only one word

          This is pretty basic, but you get the idea. It would be better to
          actually call this from onSubmit:

          <html>
          <head>
          <script>
          function disallowspace(v al){
          if(/\s/.test(val)){
          alert("Too many words.");
          }
          }
          </script>
          <body>
          <input type="text" value="" onblur="disallo wspace(this.val ue)" />
          </body>
          </html>

          Comment

          • otto

            #6
            Re: Regular expression , for only one word

            Try this:

            <html>
            <head>
            <script>
            function disallowspace() {
            if(/\s/.test(document. getElementById( 'xyz').value)){
            alert("Too many words.");
            return false;
            }
            return true;
            }
            </script>
            <body>
            <form onSubmit="retur n disallowspace() ;" id="someform"
            action="some.cg i" method="get">
            <input type="text" value="" id="xyz" />
            <input type="submit" value="submit" />
            </form>
            </body>
            </html>

            Comment

            • Rob B

              #7
              Re: Regular expression , for only one word


              Slight upgrade...

              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">
              <html>
              <head>
              <style type="text/css">

              input {
              font: 12px tahoma;
              border: 1px dashed #c88;
              padding: 4px;
              }

              </style>
              <script type="text/javascript">

              function xwhite(obj)
              {
              var v;
              if (/ /.test(v = obj.value))
              obj.value = v.replace(/ +/g,'');
              }

              </script>
              </head>
              <body onload="documen t.forms[0].elements[0].focus()">
              <form>
              <input type="text" name="t1" value=""
              onkeyup="xwhite (this)" onblur="xwhite( this)" />
              &amp;rarr; [one word only]
              </form>
              </body>
              </html>


              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              Working...