Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find All the controls in a form using jQuery or javascript

I am a starter in jQuery . How to find all the controls in a form using jQuery?

I know the code is like this

function submitValidator(){
   $("form :input").each(function(){ });

I want to access their Id's and need to apply regular expressions

Some of the text boxes are numeric remaining will be alphanumeric. Is there any method to sort them to apply regular expressions?

like image 977
rosa mandez Avatar asked Jul 25 '13 12:07

rosa mandez


People also ask

What is find () in jQuery?

jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

How do I search for a word in jQuery?

We can use the replace() method with either regular expression or string as an argument. How to find the number of occurrences of a particular word or a string in a given string or paragraph. In this case, we will use the indexOf() Method in JavaScript to find the number of occurrences of desired word or substring.


1 Answers

You can add a new property data-charSet in HTML

<input type="text" id='amt' data-charSet='numeric'>

add which all controlles you want to add after the "form :"

function submitValidator(){
                   $("form :text, textarea").each(function(){         
                        var NumericOnly = "^[0-9]*$";
                        var svId = $(this).attr('id');
            if($(this).attr('data-charSet') == 'numericonly')
                    {
                         if(!$(this).val().match(NumericOnly)) { 
                            alert("numeric");
                            eval("$('#" + svId +"').focus();")
                            return false;
                            }
                    }
                    });
            }
like image 191
Navajyoth CS Avatar answered Sep 29 '22 15:09

Navajyoth CS