Guest balagod Posted December 27, 2005 Share Posted December 27, 2005 i create one text field with name.id,value also get in PHP variables in databasehow can i check the validation of that text field in javascript <input type="text" name=" <? $row[0] ?>" id ="<? row[0]" />in js functiondocument.form.<? $row[0] ?> .value is working or nothow i can get value Link to comment Share on other sites More sharing options...
hacknsack Posted December 27, 2005 Share Posted December 27, 2005 Since you are working with field names that are dynamic how about assigning a class.You can cycle through the fields and check values accordingly, like: <html><head> <script type="text/javascript"> function ckFields(form){ for(var j = 0; j < form.elements.length; j++){ var field = form.elements[j]; if(field.type == 'text' && field.className == 'textStr' && !ckStr(field)){ return false; } if(field.type == 'text' && field.className == 'textNum' && !ckNum(field)){ return false; } } return true; } function ckStr(field){ // whatever test you need here if(field.value.length == 0){ alert("You forgot something..\n" + field.name.replace(/_/, ' ') + " needs a value"); return false; } return true; } function ckNum(field){ // whatever test you need here // the next line tests to see if the value is an Integer if(!/^\d+$/.test(field.value)){ alert("You forgot something..\n" + field.name.replace(/_/, ' ') + " needs a value"); return false; } return true; } </script></head><body> <form name="formA" onsubmit="return ckFields(this)"> Not Required: <input type="text" name="notRequired"><br> Input 1 (Required String): <input type="text" name="Input_1" class="textStr"><br> Input 2 (Required Number): <input type="text" name="Input_2" class="textNum"><br> <input type="submit" value="Submit"> </form></body></html> Thanks, Link to comment Share on other sites More sharing options...
lugos Posted December 29, 2005 Share Posted December 29, 2005 i create one text field with name.id,value also get in PHP variables in databasehow can i check the validation of that text field in javascript <input type="text" name=" <? $row[0] ?>" id ="<? row[0]" />in js functiondocument.form.<? $row[0] ?> .value is working or nothow i can get value<{POST_SNAPBACK}> Hello,The Form object has an elements[] array that contains Input objects. So, I guess if you know the number of the element you want to access, you can try using something likedocument.forms[1].elements[2].value = 'value' If not, looping through the elements will probably be your best bet; as hacknsack suggested.lugos Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now