Ajax to form field to check invalid characters?

I have this field:

<input name="name" type="text" id="name" value="<?php echo($_POST['name']); ?>">

How to do an Ajax script that checks the username doesn't contain invalid chacters and symbol or spaces and display the red warning beside it?
Who is Participating?
No nead for Ajax here.. Just create a JavaScript function that onChange fires. Use Regex (http://regexlib.com for more help) to fail or pass.

Just add an empty div next to each input and if it fails add the warning image beside it..

      function validate(field) {
            var passed = 'REGEX CODE/EXPRESSION HERE'
            var warningObj = document.getElementById(field.name + "_warning");

            if (!passed)
                  warningObj .innerHTML = "<img src='warning.gif'>";
                  warningObj .innerHTML = "";

<input type="text" name="userName" onChange="validate()"><span id="userName_warning"></span>

Alternativly you could also just create the warning image next to each item and default with CSS the the display value to "none". Then just set that to block or none depeding on the results.
Use the following code to check invalid characters. It allows only the upper case and lower case characters and numbers.

      // After enter the value in the textbox called through Ajax
      if(isset($_REQUEST['value'])) {
            $value = $_REQUEST['value'];
            if (preg_match("/^[a-zA-Z0-9]{1,}$/", "$value")) {
                  echo "<input name='name' type='text' id='name' value='$value' onchange='check_valid();'>";
                  echo "<br>Valid Characters";
            else {
                  echo "<input name='name' type='text' id='name' value='$value' onchange='check_valid();'>";
                  echo "<br><font color=red>Invalid Characters</font>";
      <!-- Ajax common script it is used in realnigerian copy from that -->
      <script src="scripts/prototype.js" type="text/javascript"></script>
      function check_valid() {
            // In which the textbox is display ("id value for the td textbox")
            var target = 'textname';
            var url = 'ajaxsymbol.php';
            var params = 'value='+document.validform.name.value;
            new Ajax.Updater(target,url,{ parameters:params });
<form name="validform" action="ajaxsymbol.php" method="post">
                  <td id="textname"><input name="name" type="text" id="name" value="<?php echo($_POST['name']); ?>" onchange="check_valid();"></td>
jtcyAuthor Commented:
Where can I get this:

<script src="scripts/prototype.js" type="text/javascript"></script>
You can download this from following url,
it's working or not? You need any other clarification?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.