5 Replies Latest reply: Dec 19, 2012 2:52 PM by winrol RSS

    Spry Validation on Text field

    winrol Community Member

      Hi, I am trying to figure out how to validate a text field so there are no spaces allowed only alph characters.

       

      Here is my spry validaton field.

       

      var sprytextfield6 = new Spry.Widget.ValidationTextField("spryRegisterLastname");

       

      I've been reeding about the useCharacter mask, How would I go about using that. Like So?

       

      var sprytextfield6 = new Spry.Widget.ValidationTextField("spryRegisterLastname", "none", {characterMasking: /dA-Z/ ,useCharacterMasking:true, validateOn:["blur", "change"]});

       

      When I use the above code it does not work.

       

      Any help would be appreciated

       

      Winrol

        • 1. Re: Spry Validation on Text field
          Ben Pleysier CommunityMVP

          First create a little JavaScript function that contains a regular expression ( see here http://regexlib.com/?AspxAutoDetectCookieSupport=1) to validate that there are no spaces

          <script>

          function ValidateNoSpaces(value) {

            return /^[a-zA-Z]+$/.test(value)

          }

          </script>

          Then apply a custom validation to the constructor

          var sprytextfield6 = new Spry.Widget.ValidationTextField("spryRegisterLastname", "custom", {validation:ValidateNoSpaces, validateOn:["blur", "change"]});

          • 2. Re: Spry Validation on Text field
            winrol Community Member

            Thanks Gramps, that worked, but...

            I was thinking and what if someone had a 2 part last name, ie Van Houte?  I get a error when I go through my form. So I though if I added a min char length to the script. Like so:

             

            What I want is to have the script assign a temp val then check the val to see if less than 5 AND there are spaces then error else pass no error. I have tested this with adding alert messages to see if I was getting what I want.  I was except when I get down to the if(/^[a-zA-Z]... part. How would I write the validation for the checking the variable for spaces? 

             

            <script>

            function ValidateNoSpaces(value) {

                      var tempstr = value.length;

                      if(tempstr < "5") {

                    if (  /^[a-zA-Z]+$/.test(tempstr)="false") {

                                                    return /^[a-zA-Z]+$/.test(value);

                                     }

                      }

            }

             

            </script>

             

             

            Also, is it possible to  run the spry validation after as I don't get any notification of what is wrong.  The text box just changes to a red color.

             

            Thanks Winrol

            • 3. Re: Spry Validation on Text field
              Ben Pleysier CommunityMVP

              It is best to use a regular expression. Think of a name like 'de la Rue'. Have a look at the last example here http://regexlib.com/Search.aspx?k=surname&c=-1&m=-1&ps=20

               

              for the second part of your question, add the highlighted line

                <span id="spryRegisterLastname">

                  <label for="text1"></label>

                  <input type="text" name="text1" id="text1">

                  <span class="textfieldRequiredMsg">A value is required.</span>

                  <span class="textfieldInvalidFormatMsg">Not the required format.</span>

                </span>

              • 4. Re: Spry Validation on Text field
                winrol Community Member

                Thanks Gramps that works awesome.

                • 5. Re: Spry Validation on Text field
                  winrol Community Member

                  Hey Gramps, after playing with this for a bit I noticed that everthing works fine but when I add a space anywhere inside the field I get and Invalid error.  I want to allow someone to enter a space then another word I just don't want client entering spaces to complete the field.

                   

                  Thanks Winrol