How to Use AJAX Validators
AJAX is a programming language based on JavaScript and XML designed to simplify website creation via easy-to-implement scripts. An AJAX validator is an AJAX script that allows you to validate something on your website. For example, if your website includes a registration form that only individuals aged 13 and over can use, you can require the user to input his birthday and then use an AJAX validator to determine whether or not the birthday is valid for the user to proceed.
Instructions
-
-
1
Create an action class that has the validation information AJAX will use to review the pertinent information for whether or not it is valid based on your parameters. Add the following code to the top of your validation file:
public class NAME extends ActionSupport {
Replace "NAME" without whatever name you'd like, such as "Age Validator".
-
2
Create your validation strings under the line you created in Step 1. The strings should contain the name of your string and integer and provide the information that the AJAX validator has to review. For example, if you want to create a validator that looks at the user's name, you can use the following string:
String name;
int NAME;
String answer;public String getName() {
return NAME;
} -
-
3
Open Notepad to create a validation.XML file that will be used in conjunction with the action class. You'll need to make "field name" the same as the name used in the action string; "field-validator type" should be "requiredstring." Your code will look something like this:
<validator>
<field name="age">
<field-validator type="requiredstring">
<message>You must enter your age!</message>
</field-validator>
</field>Press "Ctrl-S" to open the Save dialogue. Type "validation.xml" without quotes into the "Name" field and then click "Save."
-
4
Open Notepad again and press "Ctrl-S" to open the Save window. Type "validation.js" into the Name field and then click "Save." Add the following code to the file:
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script>
Press "Ctrl-S" to save the file and then close Notepad.
-
5
Right-click on your Web page file and select "Open With" and choose "Notepad." Copy and paste the following JavaScript code between the "<HEAD>" tags:
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/utils.js" type="text/javascript"></script>
Press "Ctrl-S" to save the changes; then close Notepad.
-
6
Copy the JavaScript and XML file into your website host's server where the rest of your website's files are located.
-
1