Skip to content

Instantly share code, notes, and snippets.

@edomaru
Last active August 29, 2015 14:22
Show Gist options
  • Save edomaru/672b4d043c6e837194fc to your computer and use it in GitHub Desktop.
Save edomaru/672b4d043c6e837194fc to your computer and use it in GitHub Desktop.
jQuery Command for form

Type in one input field and show in other input field realtime. Also replace non alpha numeric with empty space and replace space with underscore.

$('body').on('keyup', '.input-label', function(event) {
     var me = $(this);
     var valOfme = me.val();   
     valOfme = valOfme.replace(/[^a-zA-Z 0-9]+/g,'').replace(/\s+/g,'_');
     me.closest('tr').find('input.input-field').val(valOfme);   
});

Allow only alhpa numeric and underscore, backspace or delete key

$('body').on('keypress', '.input-field', function (e) {   
  var keycode =  e.keyCode ? e.keyCode : e.which;
  if (keycode != 8 && keycode != 46) {        
    var regex = new RegExp("^[a-zA-Z0-9_]+$");
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    }

    e.preventDefault();
    return false;
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment