gpt4 book ai didi

javascript - 提交时文本区域验证

转载 作者:行者123 更新时间:2023-12-02 19:27:26 25 4
gpt4 key购买 nike

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

Form contains invalid data. Please correct first

最佳答案

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

错误用法:

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

注意,没有任何返回

正确使用

function isThisCorrect() {
if(gCorrect) {
return true;
}
return false;
}
var boolReturn = isThisCorrect();

关于javascript - 提交时文本区域验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11920378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com