Solved

How to validate all fields with a certain name

Posted on 2016-10-06
8
18 Views
Last Modified: 2016-10-25
I use the below IF statement to validate a single text field input box called "PhoneNum" on submit. It allows either blank, or 10 digits. I need to apply this logic to all fields with the word "PhoneNum" in the text box name. The fields are added dynamically, so I need to look for all input boxes with "Phonenum" in the name.

I'd like to keep it in an if statement, and use jQuery to do this, with something like: $("[name*=PhoneNum]").....

Could someone assist?
var PhoneNum = $("#PhoneNum").val();

	    if(/^(\d{0}|\d{10})$/.test(PhoneNum) == false) {
	    	alert("All Phone Number fields can only contain 10 digits.");
	    	$( "#PhoneNum" ).focus();
	        return false;
	    	}

Open in new window

0
Comment
Question by:earwig75
  • 5
  • 3
8 Comments
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
try this:

$([name*="PhoneNum"]).each(function(){
var PhoneNum = $( this .val();

	    if(/^(\d{0}|\d{10})$/.test(PhoneNum) == false) {
	    	alert("All Phone Number fields can only contain 10 digits.");
	    	$(  this  ).focus();
	        return false;
	    	}
});

Open in new window

0
 

Author Comment

by:earwig75
Comment Utility
This doesn't seem to work. Below is what my function looks like.

<script type="text/javascript">
	window.onload = function() {
    document.getElementById("Submit").onclick = function() {
$([name*="PhoneNum"]).each(function(){
var PhoneNum = $( this .val();

	    if(/^(\d{0}|\d{10})$/.test(PhoneNum) == false) {
	    	alert("All Phone Number fields can only contain 10 digits.");
	    	$(  this  ).focus();
	        return false;
	    	}
});
</script>

Open in new window

0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
can you provide the html mark up for a few of the fields?
0
 

Author Comment

by:earwig75
Comment Utility
<input type="text" name="PhoneNum">
<input type="text" name="aPhoneNum">
<input type="text" name="PhoneNum3">
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
looks like a missing ) on line 5, it should be:

var PhoneNum = $( this ).val() ;
0
 

Author Comment

by:earwig75
Comment Utility
Thanks but that still didn't fix it.

I ended up having to create a new function outside of the submit, and then returning the function in the submit.
0
 
LVL 32

Accepted Solution

by:
Big Monty earned 500 total points (awarded by participants)
Comment Utility
try this fiddle, it works for me:

https://jsfiddle.net/bigmonty/xym4vcur/1/
0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
working example
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

771 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

11 Experts available now in Live!

Get 1:1 Help Now