How to trigger ajax by entering multiple comma-separated records in the input box of javascript

We sometimes let the user enter a lot of records in a space where html is a textarea, such as the employee's number. As the record is entered it needs to interact with the background. But we want the number of interactions to be reasonable and reduce the pressure on the backend system.

Scenario example: We have a web page that requires the user to enter the mobile phone number of the person in the textarea. You can enter multiple numbers separated by English commas. In the keyboard keyup event of the textarea, we want the user to enter the mobile phone number (usually the shortest is 11 digits, maybe In addition to the previous international number +86 and other mobile phone numbers, the length will exceed 11 digits). When the input content is greater than or equal to 11, I want to initiate a request in the background. If the user continues to enter the English separator after this number, the background request will not be initiated, and the user will continue to initiate a request in the background when the input content is greater than or equal to 11. Check the validity of the input content in turn, and initiate a request to the background. We require that the number of requests be as low as possible.

I assume here that the jquery framework is used, and the code is as follows:

$('#textareaId').bind('keyup', function() {
    var teamMembersEmpIdsRegex = / ^ (?: \ d {11,},?) + $ /;
    var val = $.trim($(this).val());
    var size = val.length;
    var postion = val.lastIndexOf(',');
    var flag = (postion == size -1); // find comma as end of string
    // The length of the trigger string is specified in the teamMembersEmpIdsRegex regular expression
    // When the end of the string is an English comma, no ajax request is made
    if (teamMembersEmpIdsRegex.test(val) && !flag) {
        debug('hello11==world ', val,' flag:',flag,'  val.lastIndexOf(','):',val.lastIndexOf(',') ,'  val.length:',val.length); // TODO
        // ajax code here
    }
});

The focus is on two places: regular expressions and the judgment of the last comma. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488538&siteId=291194637