0

I have very basic knowledge about JS/jQuery. I need to perform check on the value of certain text boxes. The example just recreates the problem I heave with the real application which is a lot bigger and the tables are dynamically created based on a data from a data base, so in run time I add the classes on which I base my jQuery logic.

Here is the jsfiddle example - http://jsfiddle.net/Xhnbm/

There are three different checks I need to do - for float, for string length and for float, however you may see that the validations is not performed I always get the integer exception and even if you remove the integer check the validation is not working either, so I guess the problem is not in the functions themselves but somewhere else.

Thanks for help.

P.S

It seem I can't submit the question without adding code. All the code is in the jsfiddle example but since I need to add some here too, I think that the problem is either in the way I declare may functions that perform the check or here :

$('#submitDocument').click(function () {
            try {
                if ($(".checkString16").val().length > 16) {
                    throw "The text can be up to 16 symbols";
                } else if (!mathFunctions.isInt($(".checkULong").val())) {
                    throw "Insert integer";
                } else if (!mathFunctions.isFloat($(".checkFloat").val())) {
                    throw "Insert float";
                }
                validationResult = true;
            } catch (exc) {
                alert(exc)
                validationResult = false;
            }
            return validationResult;
        });

in the way I try to execute the validation when the submit button is clicked. But being no Js programmer at all I don't want to point you to wrong directions.

4

1 回答 1

1
$('#submitDocument').click(function () {
        var validationResult = true;
            if ($(".checkString16 input[type='text']:first").val().length > 16) {
                validationResult=false;
            } else if (!mathFunctions.isInt($(".checkULong input[type='text']:first").val())) {
                validationResult=false;
            } else if (!mathFunctions.isFloat($(".checkFloat input[type='text']:first").val())) {
                validationResult=false;
            }
        return validationResult;
    });

这应该可以解决问题。如果 #submitDocument 是输入 type='submit' 并且您使用它来提交表单,您应该尝试这种方式:

$('#submitDocument').click(function (e) {
            if ($(".checkString16 input[type='text']:first").val().length > 16) {
                e.preventDefault();
            } else if (!mathFunctions.isInt($(".checkULong input[type='text']:first").val())) {
                e.preventDefault();
            } else if (!mathFunctions.isFloat($(".checkFloat input[type='text']:first").val())) {
                e.preventDefault();
            }
    });

e.preventDefault() 将阻止提交按钮执行默认处理程序,即提交表单。

您始终可以使用 $("#formId").submit(); 从代码中手动提交表单

PS。确保您也在后面的代码中验证这些值,因为简单的 jquery 验证很容易实现。

于 2013-05-28T07:06:04.187 回答