question about whitespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dwayne Epps

    question about whitespace

    I've created a function that checks form fields that only will have letters.
    This is the script:

    <script type="text/javascript" language="javas cript">
    function validateString( field, msg, min, max) {
    if (!min) { min = 1 }
    if (!max) { max = 65535}
    if (!field.value || field.value.len gth < min || field.value.max > max) {
    alert(msg);
    field.focus();
    field.select();
    return false;
    }
    return true;
    }
    </script>

    How can I add code to this function that checks for whitespace...ex .
    "\n\r\t"
    I would also like to check for only letters in the fields and make sure no
    numbers have been entered. Any help is appreciated. Thanks in advance.
    -D-


  • Richard Hockey

    #2
    Re: question about whitespace

    Using regular expressions would be the easiest method:

    /^[a-zA-z]+$/ only allow letters

    <script type="text/validate">
    function Validate()
    {
    // set pattern for text only
    var textRE=/^[a-zA-z]+$/;

    varTextObject1= document.forms['myform'].elements['mytextbox'];

    // check contents of textbox 'mytextbox' in form 'myform' against pattern
    if(!textRE.test (TextObject1.va lue)
    {
    alert('text field 1 can only accept text, not numbers or other
    characters.');
    TextObject1.foc us();
    return false;
    }
    return true;
    }
    </script>

    .......

    <form name="myform" action="blap.ph p" method="get" onSubmit="retur n
    Validate();">
    <input type="text" name="mytextbox ">
    <input type="submit" value="submit form">
    </form>


    for numbers only use /^[0-9]+$/
    for mixed text and numbers, with spaces /^[0-9a-zA-Z\s]+$/

    "Dwayne Epps" <dwayneeppsNOSP AM@centurytel.n et> wrote in message
    news:5TnQa.2599 $Jk5.1840087@fe ed2.centurytel. net...[color=blue]
    > I've created a function that checks form fields that only will have[/color]
    letters.[color=blue]
    > This is the script:
    >
    > <script type="text/javascript" language="javas cript">
    > function validateString( field, msg, min, max) {
    > if (!min) { min = 1 }
    > if (!max) { max = 65535}
    > if (!field.value || field.value.len gth < min || field.value.max > max) {
    > alert(msg);
    > field.focus();
    > field.select();
    > return false;
    > }
    > return true;
    > }
    > </script>
    >
    > How can I add code to this function that checks for whitespace...ex .
    > "\n\r\t"
    > I would also like to check for only letters in the fields and make sure no
    > numbers have been entered. Any help is appreciated. Thanks in advance.
    > -D-
    >
    >[/color]


    Comment

    • edoardo

      #3
      Re: question about whitespace

      "Dwayne Epps" <dwayneeppsNOSP AM@centurytel.n et> wrote in message news:<5TnQa.259 9$Jk5.1840087@f eed2.centurytel .net>...[color=blue]
      > I've created a function that checks form fields that only will have letters.
      > This is the script:
      >
      > <script type="text/javascript" language="javas cript">
      > function validateString( field, msg, min, max) {
      > if (!min) { min = 1 }
      > if (!max) { max = 65535}
      > if (!field.value || field.value.len gth < min || field.value.max > max) {
      > alert(msg);
      > field.focus();
      > field.select();
      > return false;
      > }
      > return true;
      > }
      > </script>
      >
      > How can I add code to this function that checks for whitespace...ex .
      > "\n\r\t"
      > I would also like to check for only letters in the fields and make sure no
      > numbers have been entered. Any help is appreciated. Thanks in advance.
      > -D-[/color]


      Hi!
      Well, did you know the regular expressions?!
      Take a look!

      funcion noNumbers( field ) {
      if( field.value.mat ch( /\d/ ) {
      alert('The string MUST not contain numbers!');
      }
      }

      function removeSpaces( field ) {
      field.value.rep lace( /\s/g, '' );
      }

      Here are some useful regular expressions:
      [a-zA-Z] any letter
      \d any number; same as [0-9]
      \D any NOT number; same as [^0-9]
      \w any alphanumeric character; same as [a-zA-Z-0-9_]
      \W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
      \s any whitespace (tab, space, newline, etc...)
      \S any NON-whitespace
      \n newline
      \t tab

      Try search informations about regular expressions in JavaScript!

      HTH
      --
      Edoardo
      [http://edoardoontheweb.interfree.it/index.html]

      Comment

      • Dwayne Epps

        #4
        Re: question about whitespace

        Hi Eduardo,
        Thanks for the replying to my post. I appreciate the help. I was
        looking over the regular expressions as you suggested and got the check for
        whitespace and numbers to work, but I am having difficulty validating
        against irregular characters, i.e., !#$%^&*()+=?).

        I'm using the function listed below to validate the form fields. So, if I
        wanted to validate against irregular characters, what regular expression
        could I use to check for the following irregular characters: !#$%^&*()+=?).
        Thanks again for your help! I'm pretty new with Javascript, so I really
        appreciate the help on this.

        function noNumbers(field ) {
        if (field.value.ma tch(/\d/)) {
        alert('This field can not contain numbers!');
        field.focus();
        field.select();
        return false;
        }
        return true;
        }

        -D-


        "edoardo" <edoardoonthewe b@interfree.it> wrote in message
        news:85c9e683.0 307132330.1346e f1e@posting.goo gle.com...[color=blue]
        > "Dwayne Epps" <dwayneeppsNOSP AM@centurytel.n et> wrote in message[/color]
        news:<5TnQa.259 9$Jk5.1840087@f eed2.centurytel .net>...[color=blue][color=green]
        > > I've created a function that checks form fields that only will have[/color][/color]
        letters.[color=blue][color=green]
        > > This is the script:
        > >
        > > <script type="text/javascript" language="javas cript">
        > > function validateString( field, msg, min, max) {
        > > if (!min) { min = 1 }
        > > if (!max) { max = 65535}
        > > if (!field.value || field.value.len gth < min || field.value.max > max) {
        > > alert(msg);
        > > field.focus();
        > > field.select();
        > > return false;
        > > }
        > > return true;
        > > }
        > > </script>
        > >
        > > How can I add code to this function that checks for whitespace...ex .
        > > "\n\r\t"
        > > I would also like to check for only letters in the fields and make sure[/color][/color]
        no[color=blue][color=green]
        > > numbers have been entered. Any help is appreciated. Thanks in advance.
        > > -D-[/color]
        >
        >
        > Hi!
        > Well, did you know the regular expressions?!
        > Take a look!
        >
        > funcion noNumbers( field ) {
        > if( field.value.mat ch( /\d/ ) {
        > alert('The string MUST not contain numbers!');
        > }
        > }
        >
        > function removeSpaces( field ) {
        > field.value.rep lace( /\s/g, '' );
        > }
        >
        > Here are some useful regular expressions:
        > [a-zA-Z] any letter
        > \d any number; same as [0-9]
        > \D any NOT number; same as [^0-9]
        > \w any alphanumeric character; same as [a-zA-Z-0-9_]
        > \W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
        > \s any whitespace (tab, space, newline, etc...)
        > \S any NON-whitespace
        > \n newline
        > \t tab
        >
        > Try search informations about regular expressions in JavaScript!
        >
        > HTH
        > --
        > Edoardo
        > [http://edoardoontheweb.interfree.it/index.html][/color]


        Comment

        • edoardo

          #5
          Re: question about whitespace

          "Dwayne Epps" <dwayneeppsNOSP AM@centurytel.n et> wrote in message news:<KDEQa.261 5$Jk5.1889374@f eed2.centurytel .net>...[color=blue]
          > Hi Eduardo,
          > Thanks for the replying to my post. I appreciate the help. I was
          > looking over the regular expressions as you suggested and got the check for
          > whitespace and numbers to work, but I am having difficulty validating
          > against irregular characters, i.e., !#$%^&*()+=?).
          >
          > I'm using the function listed below to validate the form fields. So, if I
          > wanted to validate against irregular characters, what regular expression
          > could I use to check for the following irregular characters: !#$%^&*()+=?).
          > Thanks again for your help! I'm pretty new with Javascript, so I really
          > appreciate the help on this.
          >
          > function noNumbers(field ) {
          > if (field.value.ma tch(/\d/)) {
          > alert('This field can not contain numbers!');
          > field.focus();
          > field.select();
          > return false;
          > }
          > return true;
          > }
          >
          > -D-[/color]

          Well, you can modify the function like this:

          function noNumbers(field ) {
          if( field.value.mat ch( /[!#$%^&*()+=?]/)\ ) ) {
          alert('This field can not contain numbers!');
          field.focus();
          field.select();
          return false;
          }
          return true;
          }

          The square brackets [] means that the matching returns 1 if at least a
          character present between them is found.
          You can add also \d if you want to match also numbers.

          Bye
          --
          Edoardo
          [http://edoardoontheweb.interfree.it/index.html]

          Comment

          Working...