sunilpaladugu Posted February 5, 2011 Share Posted February 5, 2011 hi all I new to php I need a script of php which needs to validate my username as fallows ->Allows only Alphabets ->need not be empty I need dynamic validation i.e using AJAX and PHP together. kindly help me to solve this Regards Sunil Quote Link to comment https://forums.phpfreaks.com/topic/226768-username-validations-in-php-and-ajax/ Share on other sites More sharing options...
esbenp Posted February 5, 2011 Share Posted February 5, 2011 You could validate the username using preg_match, something like. $username = $_GET['username']; if(preg_match('/^[a-zA-Z]+$/', $username)) { echo(json_encode(array('Response'=>1))); } else { echo(json_encode(array('Response'=>0))); } then it would be easy picking up the response using a library like jQuery, which has the $.getJSON function. <script type='text/javascript'> $(document).ready(function() { $('input[name=username]').bind('keyup', function() { $.getJSON('validation_file.php', {username: $(this).val()}, function(data) { var response = parseInt(data[0].Response); if(response === 1) { // valid } else { // not valid } }); }); }); </script> <input type='text' name='username'> first you attach the keyup event to your field, which means the following function fires whenever the user releases a keyboard key, with the field in focus. It then checks the validation file (which contains the php part) and sends the username along in a $_GET variable. The php script then validates it using reg ex an returns an response array encoded as json. Please note i have not tested this code and it is highly likely to have flaws in it Quote Link to comment https://forums.phpfreaks.com/topic/226768-username-validations-in-php-and-ajax/#findComment-1170188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.