Jump to content

How to Validate Text for Mobile

+ 1
  Maximiliano Firtman's Photo
Posted Nov 13 2010 08:45 PM

To reduce the client-side scripts and server-side trips for validation and to improve the usability of our forms, we should provide as many input validation properties as we can.

The first typical option is to define the maximum size accepted for the text input using the maxlength property, expressed as a number of characters. Many platforms automatically add a character counter while the user is typing.

Note: A Javascript library can add support for placeholder and autofocus (an HTML 5 attribute for input tags that indicates that this control has to be focused on as soon as the page is loaded) even on devices with no support for those attributes. You can download this library from http://gist.github.com/330318.

WAP CSS added the property -wap-input-format, which allows us to define the type and number of characters that the user can input (known as the input mask). Specifying an input mask will reduce the user’s error possibilities; it can yield error messages like the one shown below. The -wap-input-required attribute prevents the user from moving the focus away from a field until she has entered some text in it.

Browsers compatible with WAP CSS input constraints show some kind of error message when the user enters text in an invalid format and then tries to move the focus away from that field.
Attached Image

The content of the -wap-input-format attribute is a string mask using the special characters in the table below.

WAP CSS input format patterns
PatternUsage
aAny character, letter, number, or symbol.
AAny uppercase alphanumeric character.
nAny numeric character or symbol.
NAny numeric character.
xAny lower case alphanumeric character or symbol.
XAny uppercase alphanumeric character or symbol.
mAny character, lowercase by default, but with uppercase possible.
MAny character, uppercase by default, but with lowercase possible.
{n}{pattern}A fixed number of repeats (n) of the pattern defined. For example, 4N means four numeric-only characters.
*{pattern}Any number of repeats of the pattern defined. For example, *A means any number of uppercase alphanumeric characters. It can be used only once per pattern.
{pattern}{pattern}Pattern combination. For example, A*a means one uppercase character and then any number of any other character.


We can also escape other characters to create complex patterns, but this is not recommended because the pattern matching engines are not the same on all platforms and strange behavior can result.

Note: Internet Explorer Mobile also accepts the Boolean attribute emptyok defining whether a value is required (false) or not (true).

The next sample shows the standard way to define a required U.S. zip code text input, an optional phone number text input, and a required password numeric field:

<dl>
   <dt><label for="zip">ZIP Code</label></dt>
   <dl><input type="text" name="zip" style="-wap-input-format: '5N';
     	-wap-input-required: true" /></dl>
   <dt><label for="phone">Phone Number</label></dt>
   <dl><input type="text" name="phone" maxlength="15" style="-wap-input-format:
     	'*n';" /></dl>
   <dt><label for="password">Password</label></dt>
   <dl><input type="password" maxlength="8" name="password"
     	style="-wap-input-format: '8N';  -wap-input-required: true'" /></dl>
</dl>


Many older XHTML mobile browsers understand the nonstandard inputformat or format attribute, imported from WML. The syntax is the same as for the WAP CSS attribute. If we want to add the same feature for older Openwave-based devices, for example, we can use the inputformat attribute. Internet Explorer uses the format attribute with the same pattern inside:

<input type="text" name="zip" inputformat="5N" format="5N" maxchars="5" />


The table below lists which input formats are supported by which mobile browsers.

Text input format compatibility table

Browser/platform-wap-required-wap-input-formatHTML attributeplaceholder
SafariNoNoNoYes
Android browserNoNoNoYes
Symbian/S60 Yes, type required to get out the input. Error message shown in 3rd editionYes, doesn’t allow invalid chars and show and red style (5th edition) or an error message (3rd edition) if incorrectNoYes in 5th edition No before 5th edition
Nokia Series 40Yes, type requiredYes, doesn’t allow invalid chars or shows a message errorNoYes in 6th edition No before 6th edition
webOSNoNoNoYes
BlackBerryShow alert if missing inputFilter charactersNoNo
NetFrontShow alert at form submissionFilter characters and OK only appear when input is correctNoNo
Openwave (Myriad)Error messageNo behavior in 6.x Filter characters in 7.0InputNo
Internet Explorer Error alert at submitError alert at submitNoNo
Motorola Internet BrowserError on submitFilter text inputNoNo
Opera MobileError message in form submissionError message in blur and in form submissionNoNo
Opera MiniNoNoNoNo


Cover of Programming the Mobile Web
Learn more about this topic from Programming the Mobile Web. 

How do you take advantage of the new opportunities opening up in mobile web development? With this book, you'll learn the intricacies and pitfalls involved in building HTML and CSS-based apps that you can extend to work with particular devices, including the iPhone, Nokia, Blackberry, Android devices, and other smartphones. You'll not only learn how to deal with platform variations, finicky browsers, CSS compatibility, and other issues, but also how to create pleasant user experiences in a constrained environment.

Learn More Read Now on Safari


Tags:
1 Subscribe


0 Replies