Jquery form submit validation

五迷三道 提交于 2019-11-30 20:07:13
Gajotres

Try this please:

     $('#testForm').on('submit', function() {
         return $('#testForm').jqxValidator('validate');
     });

Bind a function to the submit event of the form. Return false in this function if any of the form fields fail validation.

For example:

$('form').on('submit', function() {
    // do validation here
    if(/* not valid */)
        return false;
});

Form validation have a wide set of Javascript and jQuery libraries... My sugestion is a simple jquery.com plugin.

PS: jqxValidator is a method from the jQWidgets framework? if you (reader) not need so heavy/complex plugin, see bellow pure jQuery, else @Gajotres writed the best solution (!).


Using only jQuery and basic Javascript.

For both, "plug library" and "writing your own validation methods", first check if the direct use of jQuery is what you need.

Here a code that do all what the question say to need: validation on blur, keyup and "when submitting".

function validate(){
    var cnv = $('#countryName').val();
    if (!$.trim(cnv)) {
        alert('Country Name is required!');
        return false;
    } else { return true; }
}

$('#testForm').submit(validate);
$('#countryName').bind('blur keyup', validate);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!