Welcome Guest, Not a member yet? Register   Sign In
Prevent the use of symbols
#3

[eluser]skunkbad[/eluser]
I think you might like this script that I wrote for client side limiting of chars. You can use it a few ways. I am usually using it with regex:

Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Character Input Limiter&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    [removed][removed]
&lt;/head&gt;
&lt;body&gt;
    <br>
    &lt;form id="myform" action=""&gt;
        <label for="element1">Alpha only</label>
        &lt;input id="element1" name="mytext1" type="text"&gt;
        <br />
        <label for="element2">Alpha numeric + some extras</label>
        &lt;input id="element2" name="mytext2" type="text"&gt;
        <br />
        <label for="element3">Numeric only</label>
        &lt;input id="element3" name="mytext3" type="text"&gt;
        <br />
        <label for="element4">Numeric + some extras</label>
        &lt;input id="element4" name="mytext4" type="text"&gt;
        <br />
        <label for="element5">Direct regular expression</label>
        &lt;input id="element5" name="mytext4" type="text"&gt;
    &lt;/form&gt;
[removed]
    /*
    DISALLOWED INPUT CHARACTERS
    */
    (function($){
        $.fn.disableChars = function(a) {

            /* bring in any modifications to allow */
            a = $.extend({
                allow: '',
                limit_to: '',
                direct_regex: null
            }, a);

            /* create the character base alpha, numeric, or alphanumeric (default) */
            if(a.limit_to == 'alpha'){
                charbase = 'a-z';
            }else if(a.limit_to == 'numeric'){
                charbase = '0-9';
            }else{
                charbase = 'a-z0-9';
            }

            /* convert allow string to regex */
            allowed = a.allow.split('');
            for ( i=0; i<allowed.length; i++){
                allowed[i] = "\\" + allowed[i];
            }
            a.allow = allowed.join('');

            /* combine character base with allowed chars and create regex object */
            if(a.direct_regex == null){
                var regex = new RegExp('[^' + charbase + a.allow + ']', 'ig');
            }else{
                var regex = a.direct_regex;
            }

            /* monitor events on designated elements */
            $(this)
            .bind('keyup blur', function() {
                if (this.value.search(regex) != '-1') {
                    this.value = this.value.replace(regex, '');
                }
            })
            .bind('contextmenu',function () {return false});

        }
    })(jQuery);

    $("#element1").disableChars({limit_to:"alpha"});
    $("#element2").disableChars({allow:".,:-() "});
    $("#element3").disableChars({limit_to:"numeric"});
    $("#element4").disableChars({limit_to:"numeric",allow:"-()."});
    $("#element5").disableChars({direct_regex:/[^-A-Z0-9~!#&*()_+:\'",.?]/ig});
[removed]
&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
Prevent the use of symbols - by El Forum - 11-24-2010, 06:41 PM
Prevent the use of symbols - by El Forum - 11-24-2010, 07:06 PM
Prevent the use of symbols - by El Forum - 11-24-2010, 07:57 PM
Prevent the use of symbols - by El Forum - 11-24-2010, 07:59 PM
Prevent the use of symbols - by El Forum - 11-25-2010, 03:03 AM
Prevent the use of symbols - by El Forum - 11-25-2010, 10:37 AM
Prevent the use of symbols - by El Forum - 11-25-2010, 03:41 PM
Prevent the use of symbols - by El Forum - 12-06-2010, 11:27 PM
Prevent the use of symbols - by El Forum - 12-07-2010, 01:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB