Validanguage Frequently Asked Questions

How do I add a check to see if a password field matches a password confirmation field?

The code below uses the Validanguage Object API to set up a validation rule for a password confirmation field. This code is used in main Validanguage demo page.
confirmPassword: {
        field: 'Password Confirmation',
        required: true,
        errorMsg: 'Please retype your password to confirm',
        validations: [
           {
              name: function() { return !!(this.value==document.getElementById('password').value); },
              onblur: true,
              onsubmit: true,
              errorMsg: 'The entered password confirmation does not match the password'
           }
        ]
     },

How do I add a maximum and/or minimum value to a numberic field?

This is very easy to do by writing a simple one-line custom validation function similar to the following:
function(text) { return (text >= 10 && text <= 20); }
You can use this function with either the Comment API or the Object API — simply change the numbers 10 and 20 in the function above to whatever limits you want. You can also remove either one if you want a minimum but not a maximum (or the reverse). An example of using this function with both APIs is available here.

How do I add a custom validation function?

Let's discuss the previous example in further detail. You want to use the function above to make sure a text field contains a value between 10 and 20. First, you will want to define the new custom validation function. Give it a name so you can refer to it later and put it in a script tag in the <head> section of your document:
<script type="text/javascript">
    function validateBetween10and20(text) {
        return (text >= 10 && text <= 20);
    }
</script>
Now that you have this new custom validation function available to you, you can easily apply it to a field. Here's an example of how you would apply this using the HTML Comment API:
<!-- <validanguage target="textfield5" validations="validateBetween10and20" onblur="true"
          onsubmit="true" errorMsg="Value must be between 10 and 20" /> -->
Or if you wanted to define this using the Object API, you would use something like the following:
textfield5: {
        validations: [
           {
              name: validateBetween10and20,
              onblur: true,
              onsubmit: true,
              errorMsg: 'Value must be between 10 and 20'
           }
        ]
     },


How do I remove any extra spaces before or after the text in a form field before validating it?

The validanguage.stripWhitespace() transformation will remove all leading and trailing whitespace from a form field prior to validating it.

Add this transformation with the Object API via:
validanguage.el.email = {
    transformations: [
        {
            name: validanguage.stripWhitespace,
            onblur: true
        }
    ]
},
Or, you can use the Comment API via:

<!-- <validanguage target="email" onblur="validanguage.stripWhitespace" /> -->

An example of the validanguage.stripWhitespace() function appears on the username field on the main demo page.

How do I display all the error messages in a single DIV or a single alert()?

To use a single alert() or DIV to display all applicable error messages, you have 2 options.

Option 1 (For a DIV): Use a custom onerror function to append each error message to a DIV as they are called. You can examine the existing validanguage.showError() function and refer to the main Validanguage Overview for details on creating a custom onerror function. Basically, you simply redefine validanguage.settings.onerror to the name of your custom error function, which will receive the error messages one-at-a-time as the first function argument.

Option 2 (For a DIV or alert): Redefine the individual onerror functions to an empty string and use an onerror Form Transformation to display the messages. Onerror Form Transformations are passed the following 2 arguments:

1st argument / submitStatus = A boolean indicating whether or not any form fields flunked validation 2nd argument / failedValidations = An array of objects containing details on any flunked validations, including their error messages

To construct a custom function to display all the error messages or failed fields in an alert or DIV, you would first check submitStatus and if submitStatus == false, then loop through the failedValidations array and retrieve the error messages.

How do I prevent spambot submissions of my webform?

To cut down on the number of spambot submissions for a form, you can define a bogus action for your form in the HTML then use a validanguage onsubmit transformation to dynamically change it to the correct value when the user submits the form. Since most spambots don't support javascript, they will post the form to the bogus action specified in the HTML, not the correct value in the javascript. This approach has been verified to work in all modern browsers (provided they have javascript enabled).

The onsubmit transformation used to change the form action would be something similar to the following:
validanguage.el.form1 = {
    onsubmit: function() {
        this.setAttribute('action', document.location.href);
    }
},
An example of this technique appears on this demo page.

Why do I get the error "Exception in thread "main" java.lang.AssertionError" when trying to compress Validanguage using YUI Compressor?

The default Java JVM which ships with Linux and Windows is a total piece of shit. When you try to use this JVM with YUI Compressor, it chokes on the use of conditional comments in the sniffBrowser() function in validanguage. To fix this, download the Sun Microsytems JVM and, in Linux, set your default Java JVM to the Sun one using: sudo update-alternatives --config java. Once you have switched to the Sun JVM, you should be able to compress validanguage without any problems.