提交时的textarea验证

时间:2012-08-12 07:09:28

标签: javascript dojo dijit.form

我正在验证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,但是当使用提交按钮时,它会抛出

  

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

1 个答案:

答案 0 :(得分: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();