You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:移除JavaScript代码中姓名的字母及长度限制

Solution to Remove Name Validation Restrictions

Hey there! I see you need to adjust that name validation logic—let's get this sorted out quickly. The key issue here is the validateUsername function, which uses a regex that enforces only letters and a 3-20 character length. We'll tweak that regex and update the error message to match your new requirements.

What we’re changing:

  • Regex restriction: Replace the letter-only, 3-20 character check with one that allows letters and numbers, with a minimum length of 1 (instead of 3) while keeping the max 20 characters (you can remove the max limit if needed, just adjust the regex).
  • Error message: Update the text to reflect the new valid input rules.

Modified Code:

(function($){ 
    $.fn.checkAvailability = function(opts) { 
        opts = $.extend({ 
            target: '#response', 
            trigger: '#btnCheck', 
            ajaxSource: 'test.asp', 
            fireOnKeypress: true 
        }, opts || {}); 

        var $this = $(this); 

        if (opts.fireOnKeypress) { 
            $this.keyup(function() { 
                checkUsername(); 
            }); 
            $this.keypress(function(event) { 
                if (event.keyCode == 13) { 
                    event.preventDefault(); 
                    return false; 
                } 
            }); 
        }; 

        $(opts.trigger).click(function() { 
            checkUsername(); 
        }); 

        function checkUsername() { 
            if (validateUsername()) { 
                $(opts.target).html('<img src="loading.gif"> checking availability...'); 
                usernameLookup(); 
            } else { 
                // Updated error message to match new rules
                $(opts.target).html('Name can only contain letters and numbers, and be up to 20 characters long'); 
            } 
        }; 

        function usernameLookup() { 
            var val = $this.val(); 
            $.ajax({ 
                url: opts.ajaxSource, 
                data: {fn:val,s:Math.random()}, 
                success: function(html){ 
                    $(opts.target).html(html); 
                }, 
                error:function (){ 
                    $(opts.target).html('Sorry, but there was an error loading the document.'); 
                } 
            }); 
        }; 

        function validateUsername(str) { 
            // Modified regex: allows letters + numbers, 1-20 characters
            return (/^[A-Za-z0-9]{1,20}$/.test($this.val())); 
        }; 
    }; 
})(jQuery);

Quick Customization Tips:

  • If you want to remove the maximum length restriction entirely, change the regex to /^[A-Za-z0-9]+$/ (the + means one or more characters, no upper limit).
  • If you need to allow other characters (like hyphens or underscores), just add them to the regex character set, e.g., /^[A-Za-z0-9-_]{1,20}$/.

内容的提问来源于stack exchange,提问作者Rick Kap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:48:29