?
Solved

JavaScript Validation via JQuery - using an if statement

Posted on 2010-09-14
7
Medium Priority
?
552 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
[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
  • 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
Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

 

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

764 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