Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

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?

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

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?
Robert Granlund

@Julian All fields can contain a number (character [0-9]) BUT at least one of the fields MUST contain a number (character [0-9])
Julian Hansen

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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Robert Granlund


Yes.  But the following is not:
F1: (empty)
F2: 0
F3: (empty)
It has to be at least the number 1
Julian Hansen

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?
Robert Granlund

NO, that will not work.  Only Numeric values except the number Zero.  At least one of the three fields must contain a Numeric value.
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Ok got it - will post back shortly
Julian Hansen

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

Julian Hansen

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

Thanks.  It was not 100% what I needed but this helped me to understand what to do.  Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Julian Hansen

You are welcome.