Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to validate all fields with a certain name

Posted on 2016-10-06
8
Medium Priority
?
38 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
[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
  • 5
  • 3
8 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 41832457
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
ID: 41832468
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 33

Expert Comment

by:Big Monty
ID: 41832473
can you provide the html mark up for a few of the fields?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:earwig75
ID: 41832474
<input type="text" name="PhoneNum">
<input type="text" name="aPhoneNum">
<input type="text" name="PhoneNum3">
0
 
LVL 33

Expert Comment

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

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

Author Comment

by:earwig75
ID: 41832614
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 33

Accepted Solution

by:
Big Monty earned 2000 total points (awarded by participants)
ID: 41832660
try this fiddle, it works for me:

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

Expert Comment

by:Big Monty
ID: 41858254
working example
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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