How to suppress form validation messages triggered on field blur.

Is there a way to prevent the form validation check that automatically happens when I focus out of a form field?

I would like to manually trigger the validation via formObject.validate() when the form is submitted.




1 Reply

SP Sureshkumar P Syncfusion Team July 22, 2022 07:58 AM UTC

Hi Kosal,

Query 1: Is there a way to prevent the form validation check that automatically happens when I focus out of a form field?,

We have no option to prevent the validation which happens by focusing out the form field. But we can customize the form validation when adding the options (form validation rules) inside the submit button method handler to validate the form only after clicking the form submit.

Find the code example here:

// Initialize Custom placement

let option: FormValidatorModel = {

  rules: {

    // Initialize the CustomPlacement.

    User: { required: true },

    Email: { email: [true, 'Enter valid Email'] },

  },

  customPlacement: (inputElement, error) => {

    document.getElementById('error').appendChild(error);

  },

};

// Initialize Form validation

let formObj: FormValidator;

let formId: HTMLElement = <HTMLElement>document.getElementById('formId');

document.getElementById('formId').addEventListener('submit', (e: Event) => {

  e.preventDefault();

  formObj = new FormValidator('#formId', option);

  formObj.validate();

});

 

 


Find the sample here: https://stackblitz.com/edit/aqqtyy?file=index.html,index.ts

Query 2: I would like to manually trigger the validation via formObject.validate() when the form is submitted.

Yes, you can manually trigger the form validation using formObject.validate().

To know more about the manual validation, please refer to the documentation link: https://ej2.syncfusion.com/javascript/documentation/form-validator/validation-rules/#validating-a-form

Regards,

Sureshkumar P



Loader.
Up arrow icon