Solved

Form Field Validation Logic

Posted on 2016-09-08
11
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 57

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 57

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 57

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 57

Expert Comment

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

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 57

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 57

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
center navbar (navigation menu) on web page 3 60
XML loaded in a form with dropdown 6 50
Jquery Calculation 3 38
Check for Numeric Phone Field 14 44
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

738 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