简体   繁体   English

我如何让我的正则表达式用附加变量将小写字母替换为大写字母

[英]How do i get my regex to replace lowercase to uppercase with additional variables

I have an app that in the UK POSTCODE i want the client to type in their postcode and several things happen... 1) i only allows AZ and 0-9 2) only space is allowed as special character我有一个在英国邮政编码的应用程序,我希望客户输入他们的邮政编码,然后会发生一些事情...... 1)我只允许使用 AZ 和 0-9 2)只允许使用空格作为特殊字符

Im getting what i want except that it allows upper and lowercase我得到了我想要的,除了它允许大写和小写

I know that the /i allows any case so please dont point this out.我知道 /i 允许任何情况,所以请不要指出这一点。 If i remove it and the client types in for example "sw10 9ef" they are not paying attention to whats being typed in and were getting "10 9" for customers who use lowercase but correct who uses uppercase "SW10 9EF"如果我删除它并且客户输入例如“sw10 9ef”,他们不会注意输入的内容,并且会为使用小写但正确使用大写“SW10 9EF”的客户获得“10 9”

Any help would be appreciated任何帮助,将不胜感激

Lee

<script>
function postOnly(input) {
    var regex = /[^0-9A-Z ]/gi;
    input.value = input.value.replace(regex, "");
}
</script>




<div class="form-group">
        <label class="col-sm-12 col-md-12 col-lg-12 control-label"><i class="fas fa-map-marked-alt fa-lg"></i> Postal Code ( ZZZZ XXX or ZZZ XXX ) 
</label>
        <div class="col-sm-12 col-md-12 col-lg-12">
            <input type="text" value="<?php echo set_value('postal_code'); ?>" name="postal_code" onkeyup="postOnly(this)" class="form-control" placeholder="Postal Code ( ZZZZ XXX or ZZZ XXX )">
            <br>
        </div>
    </div>

So i need it to change the case from lower to upper live whilst keeping the above requirements.所以我需要它在保持上述要求的同时将案例从低到高改变。

You really shouldn't be using the front end of your application to add restrictions to the incoming data, or at least not only the front end, as it's trivial to bypass.您真的不应该使用应用程序的前端来添加对传入数据的限制,或者至少不仅是前端,因为绕过它很简单。 Taking that on board your front end should really not care weather or not a user is using upper or lower case letters in a postcode and the back end should convert lower case letters to upper case ones before further processing.考虑到这一点,您的前端应该真的不关心天气或用户是否在邮政编码中使用大写或小写字母,后端应该在进一步处理之前将小写字母转换为大写字母。

You can add a text-transform:uppercase style to the input box so that what ever the user types appears to be in upper case, but that is only a visual styling and if the user inputs the postcode in lower case then it will be returned to the back end application as lower case letters.您可以向输入框添加text-transform:uppercase样式,以便用户输入的内容看起来都是大写的,但这只是一种视觉样式,如果用户输入小写的邮政编码,则它将被返回到后端应用程序作为小写字母。

You should check:你应该检查:

  1. Your regex allow both uppercase and lowercase characters.您的正则表达式允许大写和小写字符。
  2. After checking that your input value is correct as per your requirements convert your input value to upper case.根据您的要求检查输入值是否正确后,将输入值转换为大写。

     var regex=/[^0-9A-Za-z ]/g; if(input.value.match(regex)){ input.value=input.value.toUpperCase(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM