You are here: Home » All Posts » HTML Forms » JavaScript Form Validation Script: More features

JavaScript Form Validation Script: More features

in HTML Forms

This is in continuation of the JavaScript Form Validation script in this page. In this page, we will see some of the advanced features of the script.

Showing all the form validation errors together in a message box

If you want to show all the error messages together, then just call the EnableMsgsTogether() function as shown below.

frmvalidator.EnableMsgsTogether();


Form validation messages in single message box

See the form validation demo

Showing the form validation errors on the page itself

You can display the validation errors on the page itself.

Here are the steps:

  1. Create a place on the page for displaying the errors
  2. create a DIV on the page for the errors the DIV should be named as {formname}_errorloc
    where {formname} is the name of your form.
    Example:

    <div id='myform_errorloc' class='error_strings'></div>
    
  3. Enable on-page error display
  4. Call the EnableOnPageErrorDisplaySingleBox() function to enable on page error display.
    For example:

    frmvalidator.EnableOnPageErrorDisplaySingleBox();
    frmvalidator.EnableMsgsTogether();
    

See the form validation demo here

Showing the error messages next to the input element

Form validation error message
Here are the steps:

  1. Create a place to display the error message next to the input elements
  2. For example place a DIV next to the input element and have it ID of the format:
    {formname}_{inputname}_errorloc
    Example:

    <div id='myform_Name_errorloc' ></div>
    <input type="text" name="Name" />
    
  3. call EnableOnPageErrorDisplay()
  4. frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    

Conditional Validations

Sometimes it may be required to validate certain fields only on certain conditions. For example, a textbox ‘Other’ needs to be filled only when ‘Other’ radio option is selected.

conditional validation
Here is how to add a condition to a validation:
There is an optional 4th parameter to the addValidation() function. If you pass a condition, that condition will be checked before running the validation.
Example:

frmvalidator.addValidation("hearabout_other","req","Please fill-in this textbox",
        "VWZ_IsChecked(document.forms['myform'].elements['hearabout'],'Other')");

VWZ_IsChecked() is a handy function included in gen_validatorv4.js script that can be used to check the ‘checked’ status of radio group or check box. The first parameter is the input object and the second parameter is the value.

If it is a drop down list, you can use the VWZ_IsListItemSelected() function

frmvalidator.addValidation("city_other","req","Please fill-in the city name",
        "VWZ_IsListItemSelected(document.forms['myform'].elements['city_list'],'Other')");

VWZ_IsListItemSelected(list_object, item_value)
The first parameter is the drop down list object and the second is the value of the item.

Triggering the validations when custom submitting the form

The validation script uses the form’s onsubmit event to trigger the validations. If you are submitting the form using code, for example on clicking a hyperlink, the onsubmit event is not triggered :

<a href='#' onClick="document.yourform.submit()">Submit Form</a>

You can trigger the validations by explicitly calling the form’s onsubmit like this:

<a href='#' onClick="if(document.yourform.onsubmit()){document.yourform.submit()}">Submit Form</a>

Making it a bit neater:

<a href="javascript: submitform()">Submit</a>
<script type="text/javascript">
function submitform()
{
    if(document.myform.onsubmit())
    {
        document.myform.submit();
    }
}
</script>

Next: Save your time coding forms and validations

Just Choose Validations!
Simfatic Forms is a feature-rich web form maker.
You just have to choose the validations. More info & downloads

Related posts:

  1. JavaScript Form Validation : quick and easy!
  2. Simfatic Forms Validation: save your time coding
  3. How to Submit a Form Using JavaScript
  4. How to switch the 'action' field in an HTML form dynamically
  5. Using JavaScript to access form objects when there are multiple forms
  6. How to make a web form
  7. A Form Design Software that is Quick, Easy and Simple.
  8. How to make a web form and get it online quickly
  9. The HTML Form Tag
  10. From an HTML Form to an Email Inbox
  11. How to get the value of a form element using JavaScript
  12. JavaScript Button
  13. How to set the value of a form element using Javascript
  14. The HTML Form Submit Button
  15. Can JavaScript email a form?
Emma September 16, 2011 at 6:44 am

I want do a validation for choosing value like this: frmvalidator.addValidation(“Country”,”dontselect=000″); but my drop down menu data from database. Can tell me how to do a validation for this option

Mukesh Kumar September 15, 2011 at 1:50 pm

Hi All,
Validation.js has really made my life easier. i would say it has saved my lot of time. Everythingis is fine in validation.js, but in one of my scenario i came up with a situation where i needed some flag on client side, to know where the validation is passed or not. like isValid property of asp.net validation. Any reply will be highly appreciated.

Kevin Cornish September 14, 2011 at 10:31 am

Hi, I use the script and works well, however i have a minor problem that is causing me a real headache… all I want is when the validation suceeds a message can be displayed either in the page or as a pop up to confirm that the details have been accepted and passed to the server… it’s along and boring story why i need that but it would be very much appreciated. Thank you!

prasanth September 16, 2011 at 7:29 am

on the server side script, You can redirect to a page after processing the form

Art September 12, 2011 at 1:00 am

I am using the validation JS on a website where html pages are generated via php. I have one page that checks for a radio button to be pushed, form id update, and then another page is generated that doe snot have the update form. The validation works in the initial page but I get an error alert, “Error: couldnot get Form object Update”. Is there a way to destroy the object that was created for the initial form?

Art September 12, 2011 at 11:47 pm

I found my logic error.

prasanth September 16, 2011 at 7:27 am

As far as Javascript is concerned, each page is separate. “couldnot get Form object’ means either you don’t have the form tag itself or the form name is different

Art September 11, 2011 at 10:54 pm

This is a great addition to my tool set!

Lisa Rose September 8, 2011 at 2:01 am

hi, this has to a blind error, I am sure but I am the blind one.

here is the validation code:

var frmvalidator = new Validator(“shoutit”);
frmvalidator.addValidation(“first_name”,”req”,”Please enter your First Name”);
frmvalidator.addValidation(“first_name”,”maxlen=20″, “Max length for First Name is 20″);
frmvalidator.addValidation(“last_name”,”req”);
frmvalidator.addValidation(“last_name”,”maxlen=40″, “Max length for Last Name is 40″);
frmvalidator.addValidation(“email”,”maxlen=50″);
frmvalidator.addValidation(“email”,”req”);
frmvalidator.addValidation(“email”,”email”);

the error is: Validator not defined. It’s defined right there, first line. Looked thru the rest can’t see anything else. Help?

Thanks!

Lisa Rose September 8, 2011 at 3:08 am

scratch that last comment. i found it. knew it was a blind error!

l;o)

prasanth September 16, 2011 at 7:23 am

need to include the gen_validatorv4.js file. follow the instructions in the first page.

codrut September 5, 2011 at 11:47 am

in case of VWZ_IsChecked – how could it check against any value of radio buttons group – not a specified one?

Jon Laurie August 19, 2011 at 4:07 pm

Fantastic. Thanks so much.

Serkan August 16, 2011 at 3:21 am

Hi to all

I could not to run your Conditional Validations can u help me please look my codes

frmvalidator.addValidation(“rentdur”,”req”,”Please fill-in this textbox”,”VWZ_IsChecked(document.forms['reserve'].elements['service'],’bike’)”);
frmvalidator.addValidation(“rentime”,”req”,”Please fill-in this textbox”,”VWZ_IsChecked(document.forms['reserve'].elements['service'],’bike’)”);

where is my mistake….

prasanth August 16, 2011 at 3:39 am

Install Firebug. Then try the page in Firefox. That will help you find the problem

johnyboy July 27, 2011 at 10:00 am

Is there a parameter which can be added to the validation code for alphanumeric or
alnum which would include the ‘-’ (hyphen) character. This would be useful for hyphenated names like Smith-Klein.

jake July 22, 2011 at 4:57 am

hey, i have set the java script to show up error messages next to the input element, now i am wondering how to change the font color and style of the error display text.
thanks for the script.

claud July 21, 2011 at 2:56 am

Best way is to start by saying “please” or end by saying “thank you”.

Kyle Hobson July 21, 2011 at 1:07 am

Is there away to use condition code like this

frmvalidator.addValidation(“city_other”,”req”,”Please fill-in the city name”,
“VWZ_IsListItemSelected(document.forms['myform'].elements['city_list'],’Other’)”);

With out these ‘ quotes? Because they interfere with my PHP code.

Kyle July 18, 2011 at 9:21 pm

How do I allow numeric and symbols like – but not allow alpha at the same time?

Leon September 14, 2011 at 2:55 pm

you could use regular expression.

Previous post:

Next post: