joan2006
asked on
Validate textbox value
HI ALL,
I have a textbox nd a submit button on my .aspx page.
Is there a way i can validate if the value entered in the textbox exist in a table? I would like to do this before the user clicks on the submit button.
Thanks!!
I have a textbox nd a submit button on my .aspx page.
Is there a way i can validate if the value entered in the textbox exist in a table? I would like to do this before the user clicks on the submit button.
Thanks!!
Good fit for an AJAX call.
onChange invoke an AJAX function to return true/ false from your DB. Then alert the user if it fails.
PS : The response will be as fast as your DB query here.. SO it better be a simple lookup in DB
onChange invoke an AJAX function to return true/ false from your DB. Then alert the user if it fails.
PS : The response will be as fast as your DB query here.. SO it better be a simple lookup in DB
Why not make it a drop down (<select>) instead? You would populate that drop down with the existing values in your table. That way, the validation is inherently done for you, since it's now impossible to enter a value that doesn't exist in the db.
I have done this one php you change this in ASP.NET
<?php
include('connectdb.php');
// After enter the value in the textbox called through Ajax
if(isset($_REQUEST['value' ])) {
$value = $_REQUEST['value'];
if($value != '') {
$valuequery = "SELECT * FROM users where UserName = '$value'";
$valueresult = mysql_query($valuequery);
$valuerow = mysql_num_rows($valueresul t);
// display the correct value for the value
if($valuerow > 0) {
echo "<input type=text name=test value='' onchange='check_database() ;'>";
echo "<br><font color=red><b>Value is in database</b></font>";
}
else {
echo "<input type=text name=test value='$value' onchange='check_database() ;'>";
echo "<br><font color=green><b>Value is not in database</b></font>";
}
}
exit;
}
?>
<html>
<title>Validate</title>
<head>
<!-- Ajax common script it is used in realnigerian copy from that -->
<script src="scripts/prototype.js" type="text/javascript"></s cript>
<script>
function checkform() {
with(document.validateform ){
if(test.value == '') {
alert('Please enter some value');
test.focus();
return false;
}
}
return false;
}
function check_database() {
// In which the textbox is display ("id value for the td textbox")
var target = 'textvalue';
var url = 'validate.php';
var params = 'value='+document.validate form.test. value;
new Ajax.Updater(target,url,{ parameters:params });
}
</script>
</head>
<body>
<form name="validateform" action="validate.php" method="post">
<table>
<tr>
<td id="textvalue"><input type="text" name="test" value="" onchange="check_database() ;"></td>
</tr>
<tr>
<td><input type="button" name="submit" value="Submit" onclick="return checkform();"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include('connectdb.php');
// After enter the value in the textbox called through Ajax
if(isset($_REQUEST['value'
$value = $_REQUEST['value'];
if($value != '') {
$valuequery = "SELECT * FROM users where UserName = '$value'";
$valueresult = mysql_query($valuequery);
$valuerow = mysql_num_rows($valueresul
// display the correct value for the value
if($valuerow > 0) {
echo "<input type=text name=test value='' onchange='check_database()
echo "<br><font color=red><b>Value is in database</b></font>";
}
else {
echo "<input type=text name=test value='$value' onchange='check_database()
echo "<br><font color=green><b>Value is not in database</b></font>";
}
}
exit;
}
?>
<html>
<title>Validate</title>
<head>
<!-- Ajax common script it is used in realnigerian copy from that -->
<script src="scripts/prototype.js"
<script>
function checkform() {
with(document.validateform
if(test.value == '') {
alert('Please enter some value');
test.focus();
return false;
}
}
return false;
}
function check_database() {
// In which the textbox is display ("id value for the td textbox")
var target = 'textvalue';
var url = 'validate.php';
var params = 'value='+document.validate
new Ajax.Updater(target,url,{ parameters:params });
}
</script>
</head>
<body>
<form name="validateform" action="validate.php" method="post">
<table>
<tr>
<td id="textvalue"><input type="text" name="test" value="" onchange="check_database()
</tr>
<tr>
<td><input type="button" name="submit" value="Submit" onclick="return checkform();"></td>
</tr>
</table>
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
Vee_Mod
Community Support Moderator
opens the database
runs the following sql
select * from <Table> where <form field> = <table>.field
if record set is empty..
do something.
close recordset
close db