forms – Drupal 7 inline Javascript throws error on drupal_add_js within AJAX callback

I am trying to get Javascript to measure a text field and auto submit at a character limit for an Ajax callback to be validated against a Luhn Algorithm and error out on failure to validate.

I found this code and checked the syntax in jsfiddle

  // Trigger ajax event on myitem only after 5th character typed.
     $('.myitem-class').on('keyup', function() {
       if ($(this).val().length == 5) {

From Firing ajax_callback on textfield after a minimum number of character

The Javascript can allegedly be run inline in a wrapper like this to be able to be used in an Ajax callback in a form.:

    function my_module_form($form, &$form_state) {
    // (...)
       'Drupal.ajax.prototype.commands.myFunction = function(ajax, response, status) {
     // Whatever you want to do.
     // (...)
    return $form;

From drupal_add_js within AJAX callback

Which I got to run changing function(ajax, response, status) { to function($, Drupal ) {

But when I put in the code it looks like it throws a PHP error at “).on(“

I was not able to get the inline wrapper recommended in the Drupal 6 to 7 docs to work as it fails seeing the $ as a variable

    (function ($) {
       //your code here

According to

Stating that JavaScript should be made compatible with libraries other than jQuery by adding a small wrapper around your existing code:

Right now I am stopped by the syntax errors

Does anyone know how to get this javascript inline code setup working??