如何把冒号自动在文本框为mac地址与验证(javascript或angularjs)?

icomxhvb  于 2022-11-21  发布在  Angular
关注(0)|答案(2)|浏览(223)

我需要在每两个数字后面自动加上冒号(:)。所以它看起来像:日:A4:55:FD:56:抄送
我已经写了一个逻辑,现在我可以放冒号了,但是当我按退格键时,我不能从冒号返回。
当我把光标放在已经写好的两位数上时,它允许写超过两位数,这是我不想要的。
下面是我的代码:
于飞:

<input type="text" ng-model="mac.macAddress" name="macAddress" id="macAddress" 
           maxlength="17" ng-change="macAddressFormat(mac)">

JS:

$scope.macAddressFormat = function(mac){

        var macAddress  = mac.macAddress;

        var filteredMac = macAddress.replace(/\:/g, '');
        var length  = filteredMac.length;

        if(length % 2 == 0 && length > 1 && length < 12){
            mac.macAddress  = mac.macAddress + ':';
        }
        else{
           console.log('no');
        }
    }

请告诉我我哪里错了。谢谢!

mi7gmzs6

mi7gmzs61#

我在输入中添加了一个默认值,并添加了一个按钮,如果macAddress的长度有效,它将调用下面这行代码:

macAddr.replace(/(.{2})/g,"$1:").slice(0,-1).toUpperCase();

代码:
第一次

lpwwtiir

lpwwtiir2#

好的,那么,这是我的解决方案。它过滤非十六进制,自动插入冒号,删除时跳过它们,允许插入,并处理特殊情况(比如跳过已经存在的冒号)。它很难看吗?是的,也许吧。它工作吗?是的,在我目前发现的所有情况下。我会更新任何遗漏的情况,因为它们出现了,我应该找到并修复它们。请在您自己的代码中随意使用它,赚$$$,过上好日子。

this.macForm.controls.mac.valueChanges.subscribe((mac: string) => {
            let start = this.searchField.nativeElement.selectionStart;
            let colonStartedAtRight;
            const noColons = mac.replace(/([^A-Za-z0-9])/g, '').toLowerCase();
            const isHex = (parseInt(noColons, 16).toString(16).padStart(noColons.length, ‘0’) === noColons);
            if (!isHex) { start--; }
            if (this.searchMeterForm.controls.searchMeterField.value.charAt(this.searchField.nativeElement.selectionStart) === ':') {
                colonStartedAtRight = true;
            }
            let updatedMac = mac
                .replace(/(^:|[^A-Fa-f0-9:]|:{2,})/g, '') // Restrict characters to only those in MAC addresses
                .toUpperCase();

            for (let i = 0; i < 4; i++) {
                updatedMac = updatedMac
                    .replace(/(?:^|:)([A-Fa-f0-9]{1}):/g, '$1') // Auto remove colons around single characters
                    .replace(/([A-Fa-f0-9]{2})([A-Fa-f0-9]{2}|[A-Fa-f0-9]{1})/g, '$1:$2'); // Auto insert colon every 2 characters
            }

            this.searchMeterForm.controls.searchMeterField.patchValue(
                updatedMac.substring(0, 17),
                { emitEvent: false, onlySelf: true }
            );
            const colonBeforeCount = mac.split(':').length - 1;
            const colonAfterCount = this.searchMeterForm.controls.searchMeterField.value.split(':').length - 1;
            const colonAdded = colonAfterCount > colonBeforeCount && start % 3 === 0 ? 1 : 0;
            this.searchField.nativeElement.setSelectionRange(
                start + colonAdded,
                start + colonAdded
            );
            let nowTotalColons = 0;
            for (let i = 0, length = mac.length; i < length; i++) {
                nowTotalColons += mac.charAt(i) === ':' ? 1 : 0;
            }
            if (this.searchMeterForm.controls.searchMeterField.value.charAt(this.searchField.nativeElement.selectionStart) === ':' &&
                !(this.beforeTotalColons > nowTotalColons)) {
                this.searchField.nativeElement.setSelectionRange(
                    this.searchField.nativeElement.selectionStart + 1,
                    this.searchField.nativeElement.selectionStart + 1
                );
            }
            this.beforeTotalColons = nowTotalColons;

            if (this.searchMeterForm.controls.searchMeterField.value.charAt(this.searchField.nativeElement.selectionStart - 1) === ':' &&
                colonStartedAtRight &&
                this.searchField.nativeElement.selectionStart === start) {
                this.searchField.nativeElement.setSelectionRange(
                    this.searchField.nativeElement.selectionStart + 1,
                    this.searchField.nativeElement.selectionStart + 1
                );
            }
        })

相关问题