Jump to content

form validation


Guest balagod

Recommended Posts

Guest balagod

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

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

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

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 like
document.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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...