ComponentArt Blogs
This space is available to any ComponentArt employee to write about anything.

MaskedInput Transform functionality


The format of the masked input is fully defined by the "Transform" it uses.

A Transform is a client-side JavaScript object containing just three functions:
  1. validate – A function that takes a look at what the user just typed into the masked input box, and returns a boolean value: true if text is valid, false if it isn't.
    For example, if the transform is used to accept a Visa card number, and the user types in "4506/00020002-0002", this function returns true. (It just makes sure that there are exactly 16 digits.) If the user types in "2008-10-14", the function returns false.
  2. unmask – A function that takes a look at what the user has just typed into the input box, and "unmasks" it. Typically, this means that the text is cleaned of formatting, leaving behind just the raw data. Unmask gets called even when validate does not return true, so it should not assume that the masked text it is working on is valid.
    In our example, if the user typed in "4506/00020002-0002", this function will return "4506000200020002".
  3. mask – A function that takes a look at the raw, clean data, and "masks" it: formats it for display.
    With our raw data being "4506000200020002", this function will return "4506 0002 0002 0002".

Each of these methods takes only one argument: the reference to the client-side MaskedInput JavaScript object. The function can then easily use this object to get any other information it needs. For example, here is the entire code of the Visa card transform:
 'CreditCard_VisaMasterCard':
 {
   'validate': function(maskedInput)
   {
     // Ensure the unmasked edited text has exactly 16 digits:
     return this.unmask(maskedInput).length === 16;
   },
   'unmask': function(maskedInput)
   {
     // Simply remove all the non-digits from the displayed text:
     return maskedInput.masked.value.replace(/[^\d]/g, '');
   },
   'mask': function(maskedInput)
   {
     // Insert spaces between four-digit groups:
     return maskedInput.unmasked.value.replace(/^(\d{4})(\d{4})(\d{4})(\d{4})$/, '$1 $2 $3 $4');
   }
 }

Two often used properties of the client-side MaskedInput object are maskedInput.masked and maskedInput.unmasked:
  • maskedInput.masked refers to the display textbox element that the user sees and edits. It contains the masked text.
    Although the masked text is typically not examined on the server, it can be accessed as MaskedInput's DisplayText property.
  • maskedInput.unmasked refers to a hidden input that contains the unmasked text.
    The unmasked text is often examined or manipulated on the server, where it can be accessed as MaskedInput's Text property.
Hopefully it is now clear how Transforms function. In the next blog post I will explain how you can customize them.

Share this post: email it! | bookmark it! | digg it! | reddit!

Posted by: Jovan
Posted: Wednesday, October 22, 2008 7:06 AM


Comments

Aubada said:

Hello, I have a question about this, I used a MaskedInput control to enter an email address but when a postback happens, the email is started and ended with the single quote ' and this leads to the red color (validation failed) I am using the Wizard control and the MaskedEdit is put in some WizardStep, so when moving to another step and returning back to the current step, this problem happens, would u plz help me!
# June 10, 2009 2:06 AM

Jovan said:

This is a bug which will be fixed very shortly, with the very next release.
# June 18, 2009 1:02 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Blogs On This Site
Thoughts on web user interfaces and component development.
Ramblings of a web control developer.
Web.UI news and more
Next weeks guest: A dog and a baby dog!
I'm in your base, killing your dudes.
Absurdity is it's own message.
Musings of an ex Java developer
im in ur page, hackin ur codez
ComponentArt in the Community
... and the program ran happily ever after.
Helping you help yourselves
My bugs are actually hidden features!

This Blog