Solved

Form Field Validation Logic

Posted on 2016-09-08
11
54 Views
Last Modified: 2016-09-12
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
Comment
Question by:rgranlund
  • 7
  • 4
11 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41790450
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
 
LVL 7

Author Comment

by:rgranlund
ID: 41790481
@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
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41790514
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:rgranlund
ID: 41790614
@Julian

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

Expert Comment

by:Julian Hansen
ID: 41790737
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
 
LVL 7

Author Comment

by:rgranlund
ID: 41791484
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
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41791504
Ok got it - will post back shortly
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41791507
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
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41791509
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
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 41794876
Thanks.  It was not 100% what I needed but this helped me to understand what to do.  Thanks!
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41795010
You are welcome.
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

776 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