Solved

Form Field Validation Logic

Posted on 2016-09-08
11
61 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 56

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 56

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 56

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 56

Expert Comment

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

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 56

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 56

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

679 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