Validation

Here, I will show you to validate input fields.

Example Demo 1: Email Validation

Example Code 1: Email Validation

function doGet() {

  var app = UiApp.createApplication().setTitle('Email Validation Demo');

  var panel = app.createVerticalPanel();

  var emailLabel = app.createLabel('Enter email address right/wrong and then Validate');

  var inputBox = app.createTextBox().setId('emailBox').setName('myEmail');

  var submitButton = app.createButton('Validate');

  var infoLabel = app.createLabel('Email is Valid').setVisible(false).setId('info');

  panel.add(emailLabel)

    .add(inputBox)

    .add(infoLabel)

    .add(submitButton);

  //Create Click handlet and add to the submit button

  var handler = app.createServerClickHandler('validateEmail');

  handler.addCallbackElement(panel);

  submitButton.addClickHandler(handler);

 

  panel.add(emailLabel).add(inputBox).add(submitButton);

  app.add(panel);

  return app;

}

//Function to validate email and display the response

function validateEmail(e){

  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

  var app = UiApp.getActiveApplication();

  email = e.parameter.myEmail;

  Logger.log(email);

  if(emailPattern.test(email) == false)

    app.getElementById('info').setText("Invalid Email Address").setStyleAttribute("color", "#F00").setVisible(true);

      else

        app.getElementById('info').setText("Valid Email Address").setStyleAttribute('color', '#339900').setVisible(true);

  return app;

}