Magento – Popup form validation in magento2

magento2PHP

I have a popup model with following fields

  • name
  • email
  • comment box
  • submit button with input type=”button”

I displayed this form in a popup model.

Now I need to validate these fields. Is there any solution to validate these input fields in magento 2?

    <form>
    <h3>Feedback form</h3>
    <label>Name:</label><input type="text" name="name" id="name">
    <label>Email:</label><input type="email" name="email" id="email">
    <label>Suggestion:</label>
    <select name="selectname" id="suggestion">
        <option value="">--Select--</option>
        <option value="on product">On product</option>
        <option value="on delivery">On delivery</option>
        <option value="on usability">On usability</option>
        <option value="criticism">Criticism</option>
        <option value="others">Others</option>
    </select>
    <label>Comments:</label><textarea rows="4" cols="50" id="comments"></textarea>
    <input type="button" value="submit" id="feedbackBtn">
    </form>
</div>

Best Answer

You should re-create your form with the help of this customer registration file. It has validation classes and attributes which you can use the exact same way.

vendor/magento/module-customer/view/frontend/templates/form/register.phtml

NOTE: if you want to add server side validation then you can reference this

vendor/magento/module-contact/Controller/Index/Post.php