?
Solved

JavaScript Validation via JQuery - using an if statement

Posted on 2010-09-14
7
Medium Priority
?
555 Views
Last Modified: 2012-06-27
I'm using JQUERY validation on my donation form:
http://docs.jquery.com/Plugins/Validation

I have a set of radio buttons for the amount, IE $25, $20, $15, $10, etc... I also have a radio button for "other", which auto-selects the other textfield for input.

I need to add error-checking to the form that checks:

if UnitPrice.IsChecked=true
than "Other" is required to be a floating point int > 1.

Basically my merchant processor is throwing a "empty basket" error when someone selects the radio button for other, but does not fill in a value in the Other textfield.

See existing code for the two fields below, note that the textfield already has class="number" which activates the jquery error-checking for a floating point value.
<input type="radio" name="UnitPrice" id="UnitPrice2" value="Other" onClick="SelectTheText(document.Donation.Other);" />&nbsp;Other&nbsp;&nbsp;$<input name="Other" type="text" class="number" id="Other" size="10" />

Open in new window

0
Comment
Question by:Eroots
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33673938
So, the requirement is to check the textbox (with id 'Other') value, it should be a floating point value greater than 1, if the checkbox with name 'UnitPrice' is selected. Please confirm on this.

it should be
<input type="radio" name="UnitPrice" id="UnitPrice" value="Other" onClick="validateTextField();" /> $ <input name="Other" type="text" class="number" id="Other" size="10" />

function validateTextField()
{
   var value = parseFloat(document.getElementById("Other").value);
   if ( value=="NaN" || value < 1 )
   {
       alert("invalid value");
   }
}
0
 

Author Comment

by:Eroots
ID: 33674090
the validation can't occur until after the textfield has a value, here is the order of operation:

1) User clicks radio button "UnitPrice2", which selects the radio button next to other.
2) onClick action for "UnitPrice2" puts cursor into "Other" textfield.
3) User enters value into "other"
4) onBlur action for "Other" runs validation function.
5) Error is posted as text after text field if it is empty, or is not a number (decimals accepted to 2 places).

Alternatively, I could see it possible to tie a validation function to my submit button that checks "UnitPrice2", and if it IS checked, then validate "Other" to make sure it's not empty and is a number.

I think what you posted above has a valid function, but does not work as it validates before the user has a chance to enter the value in the field.
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1500 total points
ID: 33674197
then you can call the validateTextField() method at the onblur event of 'Other' textbox.

<input type="radio" name="UnitPrice" id="UnitPrice" value="Other" onClick="document.getElementById('Other').focus();" /> $ <input name="Other" type="text" class="number" id="Other" size="10" onblur="validateTextField()" />

function validateTextField()
{
   var value = parseFloat(document.getElementById("Other").value);
   if ( value=="NaN" || value < 1 )
   {
       alert("invalid value");
   }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Eroots
ID: 33674288
can you also draw up a quick function that I can execute on form submit that makes sure that if the "UnitPrice2" radio button is checked, that there is a valid value in "other"?

The above function solves the validation of the field, but not my original problem of people being able to check the radio button and leave the field blank when the form submits.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33674329
just include this into the function from which you will call submit
if (document.getElementById("UnitPrice2").checked)
{
   //checkbox is checked
   document.forms[0].submit();
}
0
 

Author Closing Comment

by:Eroots
ID: 33692080
Had to piece together the puzzle myself still, but was able to do so via the function below. There were multiple conditions to address, and the expert was only addressing single issues.

The following was called via thae "form" tag's "onsubmit" event handler.

function validateTextField()
{
if (document.getElementById("UnitPrice2").checked)
{
   //checkbox is checked
   var value = parseFloat(document.getElementById("Other").value);
   if ( value=="NaN")
   {
       alert("You must enter a numeric monetary value when selecting other amount");
   }
   else if ( value < 5 )
   {
               alert ("There is a minimum of $5 for online contributions");
   }
   else if ((document.getElementById("Other").value.length==0) || (document.getElementById("Other").value==null))
   {
               alert("You selected Other Amount but did not enter an amount. Please enter an amount into the field.");
      }
}
}
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33692286
thanks for the points
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 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