I’m validating a textarea. It’s working fine, but when I submit the form then it is not working anymore.
My code is:
<%@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>
The above code works fine onBlur, but when the submit button is used it throws
Form contains invalid data. Please correct first
You need to use
returnin your validate function, otherwise any call to that function will return:undefinedwhich is the ‘same’ as a boolean false, for instance:wrong usage:
Note, nothing returns
correct usage