Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Ajax to form field to check invalid characters?

Posted on 2007-10-05
7
Medium Priority
?
893 Views
Last Modified: 2008-09-27
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?
0
Comment
Question by:jtcy
  • 3
5 Comments
 
LVL 4

Accepted Solution

by:
Russell2566 earned 1000 total points
ID: 20021213
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..

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

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

<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.
0
 
LVL 5

Assisted Solution

by:siva_siva
siva_siva earned 1000 total points
ID: 20021252
Use the following code to check invalid characters. It allows only the upper case and lower case characters and numbers.

ajaxsymbol.php
############
<?php
      // 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>";
            }
            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"></script>
<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 });
      }
</script>
<form name="validform" action="ajaxsymbol.php" method="post">
      <table>
            <tr>
                  <td id="textname"><input name="name" type="text" id="name" value="<?php echo($_POST['name']); ?>" onchange="check_valid();"></td>
            </tr>
      </table>
</form>
0
 

Author Comment

by:jtcy
ID: 20021628
Where can I get this:

<script src="scripts/prototype.js" type="text/javascript"></script>
0
 
LVL 5

Expert Comment

by:siva_siva
ID: 20021680
You can download this from following url,
http://www.prototypejs.org/assets/2007/5/1/prototype.js
0
 
LVL 5

Expert Comment

by:siva_siva
ID: 20027131
it's working or not? You need any other clarification?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question