Sencha Touch Cookbook
上QQ阅读APP看书,第一时间看更新

Putting custom validation in the e-mail field

This recipe describes how to make use of the e-mail form field provided by Sencha Touch and how to validate the value entered into it to find out whether the entered e-mail passes the validation rule or not.

Getting ready

Make sure that you have set up your development environment by following the recipes outlined in Chapter 1.

Make sure that you have followed the Getting your form ready with FormPanel recipe in this chapter.

How to do it...

Carry out the following steps:

  1. Copy ch02_01.js to ch02_03.js.
  2. Open a new file named ch02_03.js and replace the definition of formBase with the following code:
    var formBase = {
      items: [{
        xtype: 'emailfield',
        name : 'email',
        label: 'Email',
        placeHolder: 'you@sencha.com',
        useClearIcon: true,
        listeners: {
          blur: function(thisTxt, eventObj) {
            var val = thisTxt.getValue();
    
            //validate using the pattern
            if (val.search("[a-c]+@[a-z]+[.][a-z]+") == -1)
              Ext.Msg.alert("Error", "Invalid e-mail address!!");
            else
              Ext.Msg.alert("Info", "Valid e-mail address!!");
          }
        }
      }]
    };
  3. Include ch02_03.js in place of ch02_02.js in index.html.
  4. Deploy and access the application in the browser.

How it works...

The e-mail field can be constructed by using the Ext.form.Email class instance or by using the xtype: emailfield. The e-mail form field implements HTML5 <input> with type="email." However, as with the search field, the implementation is very limited. For example, the HTML5 e-mail field allows us to specify a regular expression pattern which can be used to validate the value entered in the field.

See also

  • The recipe named Setting up the Android-based development environment in Chapter 1
  • The recipe named Setting up the iOS-based development environment in Chapter 1
  • The recipe named Setting up the Blackberry-based development environment in Chapter 1
  • The recipe named Setting up the browser-based development environment in Chapter 1
  • The recipe named Setting up the production environment in Chapter 1
  • The recipe named Getting your form ready with FormPanel in this chapter