Solved

How do I display an error message? -- <select> using jQuery form validation

Posted on 2012-12-28
16
466 Views
Last Modified: 2013-01-07
Hi,
 I have a form using jQuery validation. The form displays an error message for each required <input> if the user hasn't filled it out. For the <select> items, the form validates, but it doesn't display an error message if the user hasn't selected an item from the from the dropdown. Can you write a rule that would display a message for the two form selects in my form. See attached code. I just want the message to say something like "Please select an item".
Thanks!
form-page.html
0
Comment
Question by:ron4721
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
16 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38728797
If you want to use the standard validation, to validate for not filled in or selected, the value has to be empty.

You have value="0"

<select id="state-terr" name="state-terr" class="required" title="Please select a State/Province/Territory.">
  <option value="0" selected="selected">--Select--</option>

So EITHER make that empty OR write a custom validation (could be as simple as testing that selectedIndex <1)
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 38729938
Personally, I use the jQuery ValidationEngine plugin. Very easy to use and does this for you.

http://github.com/posabsolute/jQuery-Validation-Engine
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38730326
I am sure that validator ALSO needs a custom validator for a select with the first option not empty.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 26

Assisted Solution

by:EddieShipman
EddieShipman earned 500 total points
ID: 38733159
It has many default validation rules.
0
 

Author Comment

by:ron4721
ID: 38745655
Hi EddieShipman,
I tried adding the jQuery ValidationEngine plugin to my page. So far i got this error in Firefox:

TypeError: form.on is not a function


any ideas?
Thanks.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38745735
Nope, what line? Do you have an accessible site where I can look at the source?
0
 

Author Comment

by:ron4721
ID: 38745744
I put these in my doc:
<script src="/js/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>

Open in new window


Then, i added this:
<script>
$(document).ready(function(){
        $("#apForm").validationEngine();
});
</script>

Open in new window


I got this error in Firefox:
TypeError: form.on is not a function
(the above error points to this line in the "jquery.validationEngine.js" file)
       
form.on(options.validationEventTrigger, "["+options.validateAttribute+"*=validate]:not([type=checkbox]):not([type=radio]):not(.datepicker)", methods._onFieldEvent);
	form.on("click", "["+options.validateAttribute+"*=validate][type=checkbox],["+options.validateAttribute+"*=validate][type=radio]", methods._onFieldEvent);
	form.on(options.validationEventTrigger,"["+options.validateAttribute+"*=validate][class*=datepicker]", {"delay": 300}, methods._onFieldEvent);

Open in new window



Hope this helps.
Thanks again.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38750995
Try this instead:
$("#apForm").validationEngine("attach", {scroll: true});

Open in new window


You need to 'attach' the validation to the form. The scroll option makes the page scroll to the field that has the error.
0
 

Author Comment

by:ron4721
ID: 38751805
Hi Eddie,
I tried the above, but i still get the same "TypeError: form.on is not a function" error.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38752479
What version of validationEngine do you have?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38752615
and do you load jQuery first?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38752632
Yeah, forgot that part!!
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38752646
It may also depend on the version of jQuery that you are using. I see that ValidationEngine version 2.6.1 has that code because I couldn't find that code in my 2.2.version.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 38752652
Would it be possible for you to mockup a page and post it on jsfiddle?
0
 

Author Comment

by:ron4721
ID: 38752679
Hi Eddie,
 I'm using version 2.6.2 of Validation Engine. I'll update my jquery to jquery-1.8.2.min.js and see if that works...i'll let you know shortly....
thanks again :)
0
 

Author Comment

by:ron4721
ID: 38752896
Hi Eddie,
 Yep! I needed to update my jquery to 1.8.2. Now it works!! And it looks great. I like how the email address gets validated without adding any extra script.  Thanks again!!!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAx/JQuery/JSON Error 3 38
Anchor Bookmark 6 33
Jquery syntax 12 29
My tooltip is not displaying correct 11 15
This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question