0

我正在验证一个textarea. 它工作正常,但是当我提交表单时,它不再工作了。

我的代码是:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <link rel="stylesheet" href="dojo/dojoroot/dijit/themes/claro/document.css">
        <link rel="stylesheet" href="dojo/dojoroot/dijit/themes/claro/claro.css" />
        <script src='dojo/dojoroot/dojo/dojo.js' data-dojo-config=' parseOnLoad: true'></script>
        <title>JSP Page</title>
        <script>
            dojo.provide("ValidationTextarea");
             dojo.require("dijit.form.SimpleTextarea");
            dojo.require("dijit.form.ValidationTextBox");  
             dojo.require("dijit.form.Form");
            dojo.require("dijit.form.Button");
            dojo.declare("ValidationTextarea",
            [dijit.form.ValidationTextBox,dijit.form.SimpleTextarea],
            {
                invalidMessage: "Invalid Input",

                postCreate: function() {
                    this.inherited(arguments);
                },

                validate: function() {
                    this.inherited(arguments);
                    if (arguments.length==0) this.validate(true);
                },

                onFocus: function() {
                    if (!this.isValid()) {
                        this.displayMessage(this.getErrorMessage());
                    }
                },

             onBlur: function() {
                    this.validate(false);
                }
            }
        );
        </script>
    </head>
    <body class="claro">
        <div data-dojo-type="dijit.form.Form" id="myForm" data-dojo-id="myForm"
             action="" method="post">
         <script type="dojo/method" data-dojo-event="onReset">
                return confirm('Press OK to reset Form values');
            </script>
            <script type="dojo/method" data-dojo-event="onSubmit">
                if(this.validate()){
                return confirm('Form is valid, press OK to submit');
                }
                else{
                alert('Form contains invalid data.  Please correct first');
                    return false;
                }
                return true;
            </script>
        <textarea id="address" name="address"  rows="4" cols="25" promptMessage="Allowed , Alphanumeric # , . - _ blank space " regExp="^[a-z0-9 #,_. -]{3,300}" dojoType="ValidationTextarea"  required="true"></textarea>

         <button data-dojo-type="dijit.form.Button" type="reset">Reset</button> 
         <button data-dojo-type="dijit.form.Button" type="submit" name="submitButton" value="Submit">Submit</button>
        </div>
    </body>
</html>

上面的代码工作正常onBlur,但是当使用提交按钮时它会抛出

表单包含无效数据。请先更正

4

1 回答 1

2

您需要return在验证函数中使用,否则对该函数的任何调用都将返回:undefined这与布尔值 false 相同,例如:

错误用法:

function isThisCorrect() {
    if(gCorrect) {
       rogerthat();
    } else {
       nope();
    }
}
var boolReturn = isThisCorrect();

注意,没有任何回报

正确使用

function isThisCorrect() {
    if(gCorrect) {
       return true;
    }
    return false;
}
var boolReturn = isThisCorrect();
于 2012-08-12T07:14:55.187 回答