Skip to content

Instantly share code, notes, and snippets.

@danwoods
Created August 15, 2011 03:23
Show Gist options
  • Save danwoods/1145660 to your computer and use it in GitHub Desktop.
Save danwoods/1145660 to your computer and use it in GitHub Desktop.
Immediate, directing validation with jQuery
Immediate feedback validation
Author: Dan Woodson
Date: 2011-08-14
for ticket GRN-1833
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Immediate, Directing Vaildation</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="instant_validation.js"></script>
</head>
<body>
<form action="/" method="get" accept-charset="utf-8">
<input type="text" name="target" value=""><br />
<input type="text" name="text" value=""><br />
<input type="radio" name="radio" value="radio_0" /> radio_0<br />
<input type="radio" name="radio" value="radio_1" /> radio_1<br />
<input type="checkbox" name="checkbox" value="checkbox" /> checkbox_0<br />
<input type="checkbox" name="checkbox" value="checkbox" /> checkbox_1<br />
<select name="select" id="select">
<option value="option_0">option_0</option>
<option value="option_1">option_1</option>
</select><br />
<textarea name="textarea" rows="4" cols="20">textarea</textarea><br />
<input type="submit" value="Continue &rarr;">
</form>
</body>
</html>
function CodeExist(code){
/*
Ajax Call to determine if code exist
*/
if(code == 'pass'){
code = false;
}
return code;
}
$('document').ready(function(){
$('[name=target]').keyup(function(){
// Clean up any old keyup residue
$('#code_exist_message').remove();
$(this).siblings(':input').removeAttr('disabled');
// See if an id already exist for this code
var code_id = CodeExist($(this).val());
if(code_id){
// Inform user of naming conflict
$(this).after('<span id="code_exist_message" style="font-size: 10px;">The code \''+$(this).val()+'\' already exist. Click <a href="http://blah.com/blah/'+code_id+'/update">here</a> if you\'d like to update it.</span>');
// Disable other inputs until user changes code
$(this).siblings(':input').attr('disabled', 'true');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment