0

how to validate the text field while submitting the form using jquery to display an alert message if the text field contains other than keyboard special characters like ascii characters form 127-255 and iso-8859-1 caharacters

Sample Code :

Javascript Code :

<script type="text/javascript">
    $(function()
    {           
        $('#send').click(function()
        {
            var firstname=$('#firstname').val();
            var pattern = "^[a-zA-Z0-9~`!@#$%^&*()_+-={}|:;<>,.?\/']+$";
            if((!firstname.match(pattern)))
            {
                alert('your input contained not supported characters');
                return false;
            }
            return true;
        });
    });
</script>

Html Code :

<form id="ajax_form" action="ajaxoutput.php">
<input type="text" name="FirstName" id="firstname" placeholder="FirstName" /><br>
<input type="text" name="LastName" placeholder="LastName" /><br>
<input type="file" name="image"/><br/>
<input type="submit" value="submit" name="submit" id="send" /> <input type="reset" value="reset"/>
</form>
4
  • yes i use that one, but it allow only alphanumeric characters, how to allow keyboard symbols also
    – lalith458
    Commented Jun 14, 2013 at 12:58
  • One way would be to add manually each one of them. I am pretty someone already did it, so you can try to google and find an already existing RegEx. Commented Jun 14, 2013 at 13:01
  • I think "only keyboard character" is imprecise, but I assume you want to say only european (or maybe american) keyboard characters
    – Jaay
    Commented Jun 14, 2013 at 13:08
  • My keyboard has all of áěěíóúůýčď�?řšťž. Commented Jun 14, 2013 at 13:08

1 Answer 1

0

You are passing a string as .match() argument whilke it need a regular expression.

To create a regexp, you can do this :

var pattern = new RegExp("^[a-zA-Z0-9~`!@#$%^&*()_+-={}|:;<>,.?\/']+$");

or

var pattern = /^[a-zA-Z0-9~`!@#$%^&*()_+-={}|:;<>,.?\/']+$/;

Then match will work!

Since you use a lot of special character, you'll probly need to escape some of them aswell (with \).

3
  • i have tried ur code, it is worked fine, but how to allow special carachters like []\ in the above code
    – lalith458
    Commented Jun 14, 2013 at 13:17
  • if i am not misstaken, you need to escape them like that : \\ \[ \] Commented Jun 14, 2013 at 13:47
  • The . must be escaped too and probly the *, the +, the $ and the ^. Well better try it and check if you need to escape them Commented Jun 14, 2013 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.