Solved

Form Field Validation Logic

Posted on 2016-09-08
11
40 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 51

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 51

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
 
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 51

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 51

Expert Comment

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

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 51

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 51

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now