Recent twitter entries...

  •  

How to add a custom filter to a Spry text field validation

Posted by Nitai | Posted in Development | Posted on 24-07-2009

9

I have to say that I love the Adobe Spry Javascript framework. It is easy to use and simply just works. For most situations it comes with “widgets” for just about everything you need in your Ajax enabled web application.

One of my favorite widgets is the Text Validation one. With it you are able to validate just about everything the user enters and you will have full control of the input, even the pattern of the input.

But what about if you need a custom validation? According to the documentation you can use the “custom” parameter with a “pattern”. But if you use it, you actually apply a “pattern”, meaning applying a pattern of “pattern: “bbb” only allows the user to enter 3 alphabetical characters, but nothing more. Usually you want to have the user enter only alphabetical characters and not limiting the amount of chars.

In order to achieve this, you have the undocumented parameter called “characterMasking”! “characterMasking” combined with “usecharacterMasking: true” allows you to pass Regular Expressions to the text field you want to validate within the Spry syntax. Powerful stuff. It is beyond me, why Adobe has not updated their documentation on this. In any case, here is a example:

Say you want to allow only alphabetical character in lowercase and nothing else, you would use a syntax of;

var custom = new Spry.Widget.ValidationTextField(“myfield”,”custom”, {isRequired:true, hint:’some hint’, characterMasking: /[a-z]/, useCharacterMasking:true, validateOn:["blur", "change"]});

Would you like to only enable digits and spaces?

var sd = new Spry.Widget.ValidationTextField(“subdomain”,”custom”, {isRequired:true, hint:’Desired Subdomain’, characterMasking: /[\d\s]/, useCharacterMasking:true, validateOn:["blur", "change"]});

I guess you get the point. Combined with Regular Expression the Spry Text Validation Widget just entered into a new level of productivity.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Comments (9)

Thank you

This is just fantastic – thank you. But why wont it work on a textarea?

It works, but Spry gives you a Textarea script and CSS. So you would need to load that as well. Check the Spry download and look in the Widget folder. There is a textareavalidation folder :-)

I already set up the sprytextarea, but when I try adding the character masking to it it does nothing. Here's my code:

<span id="spryTextArea">
Question/Comment

<textarea name="Comment" id="Comment" cols="45" rows="6"></textarea>
</span>

var spryTextArea = new Spry.Widget.ValidationTextarea("spryTextArea", "custom", {isRequired:false, characterMasking: /[A-Za-zs-d]/, useCharacterMasking:true, validateOn:["blur", "change"]});

If you can spot something I'd appreciate your help. I wasted 12 hours on this yesterday and ended up having to do a php validation once the form was submitted (not very elegant).

Basically all I want to do is try and block "http://" to cut down on spam submissions.

Hi,

Sorry, but I don't have much time right now, since we are about to release a new version of Razuna (http://razuna.org) in the next hours. But look at the examples from Adobe at http://labs.adobe.com/technologies/spry/samples/v...

A quick Google search with http://www.google.com/search?q=spry+textarea+mask... also brought up many results :-)

Kind Regards,
Nitai

Thanx for responding.

I have already read all the Adobe samples and none of them use validation against unwanted characters, only number of characters.

Of those 12 hours I spent on this at least 6 of them were searching google and reading and trying various results. None of them validate a text area against unwanted characters, only text fields, and as I said for some unknown reason it doesn't want to work on a text area.

Short time ago since there was the last comment
Your post was really helpful for me I have to thank you a lot
but I got a question I just tried the following

var titlevalidation = new Spry.Widget.ValidationTextField("titlevalidation", "custom", {isRequired:true, characterMasking: /[a-zA-Z0-9äöüÄÖÜ_]/, useCharacterMasking:true, validateOn:["submit"], minChars:5, maxChars:16}

works fine, with that little problem that i can't type special character (like "§$% and so on)
but I used validateOn:["submit"] so I should be able to type the special characters and get an error message on submit shouln't I?

kind regard, dennis

Since you use the ValidateOn=Submit it should only validate on Submit. Try turning on/off CharacterMasking and see if that makes any difference. Also check that your form is valid an no other ID tag is interfering.

Hi Nitai

Now that you are back on decks again, would you be able to have a look at the textarea issue I mentioned earlier? Thanks in advance.

Regards Arran

Write a comment