I'm a javascript newbie and have a simple form (one text field) that I need to validate and submit to a dynamic url. The text field can only have one of two values and will redirect the user to one of two urls based on what they enter in the text field:
I need to validate the entry in the text box so I'm using the Validator plugin, but I can't figure out how to implement the form submit. It's not redirecting to the url (or anything for that matter.) This is what I've got so far:
- // Token validator
- // add custom rule
- $.validator.addMethod("equals", function(value, element, string) {
- return $.inArray(value, string) !== -1;
- }, $.validator.format("Please enter '{0}'"));
- $().ready(function() {
- $("#tokenForm").validate({
- rules: {
- tokenCode: {
- required: true,
- equals: ["CODE1", "CODE2"]
- }
- },
- messages: {
- tokenCode: "Invalid token. Please try again."
- },
-
- submitHandler: function(form) {
- window.location="/"+form.tokenCode.value+".html";
- form.submit();
- }
-
- });
- });
And the html for the form:
- <form id="tokenForm" name="tokenForm" method="post" action=" ">
- <table class="webform" cellspacing="0" cellpadding="2" border="0">
- <tbody>
- <tr>
- <td><label for="tokenCode">Enter Token <span class="req">*</span></label><br />
- <input type="text" maxlength="10" name="tokenCode" id="tokenCode" /></td>
- </tr>
- <tr>
- <td><input class="cat_button" type="submit" value="Submit" /></td>
- </tr>
- </tbody>
- </table>
- </form>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
- <script src="/js/token.js"></script>
Any help is appreciated.