Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 77
  • Last Modified:

Form Field Validation Logic

I have a form with three fields.  1 or more of these fields needs to contain at least 1 number. (all three fields can only contain a number) At least one field needs to contain a number.  At the moment you can enter Zero (0) but it must be at least the number 1.  What type of jQuery validation can I do to validate these three fields for this type of rule?
0
rgranlund
Asked:
rgranlund
  • 7
  • 4
1 Solution
 
Julian HansenCommented:
Your requirements appear contradictory

1 or more of these fields needs to contain at least 1 number
all three fields can only contain a number

Are you saying that a field must contain at least one numeric character [0-9] or are you saying that all fields must contain only numbers?
0
 
rgranlundAuthor Commented:
@Julian All fields can contain a number (character [0-9]) BUT at least one of the fields MUST contain a number (character [0-9])
0
 
Julian HansenCommented:
Can they contain other characters in addition to a number?
Assuming it is only numbers then is it single digits only or can it be more than one digit?

I am assuming from what you said that the following is valid.
F1: (empty)
F2: 33
F3: (empty)
Is the above valid?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rgranlundAuthor Commented:
@Julian

Yes.  But the following is not:
F1: (empty)
F2: 0
F3: (empty)
It has to be at least the number 1
0
 
Julian HansenCommented:
And
F1:ABC
F2:A2
F3:
I am still not certain of the exact set of rules. Can fields contain non-numerics.
If a field is allowed alpha's does F2 count?
0
 
rgranlundAuthor Commented:
NO, that will not work.  Only Numeric values except the number Zero.  At least one of the three fields must contain a Numeric value.
0
 
Julian HansenCommented:
Ok got it - will post back shortly
0
 
Julian HansenCommented:
What about this.
var total = 0;
$('input').each(function(i, e) {
   total += $(e).val();
});
if (total > 0) alert('you are ok');
else alert('invalid input');

Open in new window

0
 
Julian HansenCommented:
Checking for strings
var total = 0;
$('input').each(function(i, e) {
   var val = $(e).val();
   // check if value is not a number
   if (isNaN(val)) {
       total = 0;
       // Potential alert here
       return false;
   }
   total += val;
});
if (total > 0) alert('you are ok');
else alert('invalid input');

Open in new window

0
 
rgranlundAuthor Commented:
Thanks.  It was not 100% what I needed but this helped me to understand what to do.  Thanks!
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now