1

I am looking to disable alt codes and characters (Ê,Œ) from being put into a text input. Tried using the alphanum library as well as just disabling the alt key to still haven't been able to prevent the characters.

Thoughts// suggestions?

thedj25
  • 13
  • 3

2 Answers2

0

You can try something like this https://jsfiddle.net/mykisscool/VpNMA/

$('#phone-number', '#example-form')

.keydown(function (e) {
    var key = e.charCode || e.keyCode || 0;
    $phone = $(this);

    // Auto-format- do not expose the mask as the user begins to type
    if (key !== 8 && key !== 9) {
        if ($phone.val().length === 4) {
            $phone.val($phone.val() + ')');
        }
        if ($phone.val().length === 5) {
            $phone.val($phone.val() + ' ');
        }           
        if ($phone.val().length === 9) {
            $phone.val($phone.val() + '-');
        }
    }

    // Allow numeric (and tab, backspace, delete) keys only
    return (key == 8 || 
            key == 9 ||
            key == 46 ||
            (key >= 48 && key <= 57) ||
            (key >= 96 && key <= 105)); 
})

.bind('focus click', function () {
    $phone = $(this);

    if ($phone.val().length === 0) {
        $phone.val('(');
    }
    else {
        var val = $phone.val();
        $phone.val('').val(val); // Ensure cursor remains at the end
    }
})

.blur(function () {
    $phone = $(this);

    if ($phone.val() === '(') {
        $phone.val('');
    }
});

You will need to specify the key values for the alt codes that you want to restrict. They can be found at http://www.alt-codes.net/

This solution requires jQuery.

forgetso
  • 2,194
  • 14
  • 33
0

add a regular expression validator to the text input on submit.

Use here:(http://www.w3schools.com/jsref/jsref_obj_regexp.asp)

Something to the effect of: [a-zA-z0-9\@.\ #] <-- means anything within the range of: lower case a-z, upper case A-Z, digits 0-9, and the literal at, period, space, and hashtag symbols. You can create an:

if (formValue != regExp) { //don't allow submit, or run whatever function or what have you }

Mike Horstmann
  • 580
  • 2
  • 9
  • var desired = stringToReplace.replace(/[^\w\s]/gi, '') Shared form another StackOverflower who was attempting a similar process (http://stackoverflow.com/questions/4374822/javascript-regexp-remove-all-special-characters) - includes use examples. – Mike Horstmann Aug 18 '15 at 15:10