Convert Multi Byte To Single Byte


HTML

<input type="text" class="input-text input-xxsmall order-input" (keyup)="onSkuSizeChange($event)"/>
----------------------------------
TypeScript: component

public numberOnly(str: any) {
    let regexp = /^[0-9]*$/;
    let _str = '';
    let i = 0;
    for (i = 0; i < str.length; i++) {
      let c = str.charCodeAt(i);
      if (65296 <= c && c <= 65305 || regexp.test(str.charAt(i))) {
        _str += str[i];
      } else {
        _str += '';
      }
    }
    return _str;
  }
  
----------------------------------

  /**
   * Convert MultiByte Digit to single byte digit.
    * @param str : string character.
   */
  public convertMultiByteToSingleByte(str: any) {
    let _str = this.numberOnly(str);
    let byteChar = '';
    let i = 0;
    for (i = 0; i < _str.length; i++) {
      let c = _str.charCodeAt(i);
      if (65296 <= c && c <= 65305) {
        byteChar += '' + String.fromCharCode(parseInt((c - 65248).toString(16), 16));
      } else {
        byteChar += '' + _str.charAt(i);
      }
    }
    return byteChar;
  }
  
  ----------------------------------
  
  public onSkuSizeChange(event: any) {
    let qty = event.target.value.trim();
    qty = this.convertMultiByteToSingleByte(qty);
    qty = parseInt(qty);

    event.target.blur();
    event.target.value = qty;
    event.target.focus();
  }
Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment