简体   繁体   English

使用Java验证输入文本字段

[英]Validating input text field using Javascript

I guess I'm doing a trivial error somewhere but will be grateful if someone can spot it. 我想我在某个地方犯了一个小错误,但如果有人能发现它,将不胜感激。 I am trying to validate a postcode in a form field once it has been typed in. Similar code works fine in PHP but I've spent hours and the JS does not seem to be executing whatever I do. 输入后,我将尝试在表单字段中验证邮政编码。类似的代码在PHP中可以正常工作,但是我花了几个小时,而JS似乎没有执行我所做的任何事情。 Here is part of the form (all within body tags): 这是表单的一部分(全部在body标签内):

<form name ="register" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">

...


<script  type="text/javascript" src="common.js">
             </script>

<input type="text" name="postcode" class="form-control" placeholder="Postcode" maxlength="10"  value='' onchange="isValidPostcode(this.form)"  required />

Here are versions of the javascript (stuffed with alerts just to print out something). 这是javascript的版本(填充了警报只是为了打印出一些东西)。 Version 1: 版本1:

function isValidPostcode(form) { 
    alert("called");

             var p = document.register.postcode.value;
             var postcodeRegEx = '/^([g][i][r][0][a][a])$|^((([a-pr-uwyz]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[a-hk-y]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[1-9][a-hjkps-uw]{1})|([a-pr-uwyz]{1}[a-hk-y]{1}[1-9][a-z]{1}))(\d[abd-hjlnp-uw-z]{2})?)$/i'; 
             if (postcodeRegEx.test(p)) alert("OK"); 
             else alert("This does not look a valid UK postcode...");
            }

Version 2 (is called without a parameter): 版本2(不带参数调用):

function isValidPostcode() { 
    alert("called");
             var p = document.getElementById('postcode').value.replace(/\s/g,''); 

             var postcodeRegEx = '/^([g][i][r][0][a][a])$|^((([a-pr-uwyz]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[a-hk-y]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[1-9][a-hjkps-uw]{1})|([a-pr-uwyz]{1}[a-hk-y]{1}[1-9][a-z]{1}))(\d[abd-hjlnp-uw-z]{2})?)$/i'; 
             if (postcodeRegEx.test(p)) alert("OK"); 
             else alert("This does not look a valid UK postcode...");
            }

I tried binding to other events but can't get a single alert out. 我尝试绑定到其他事件,但无法发出任何警报。 Even exact reproduction of the examples is not working. 实例的精确复制都无法正常工作。 Hope someone gives me an idea of what is wrong. 希望有人给我一个错的想法。

You should use the keyup event to do that and add the event using JS, not inline it. 您应该使用keyup事件来执行此操作,并使用JS(而不是内联)添加事件。

postcodeRegEx is a regex, not a string, you need to remove quotes around it. postcodeRegEx是一个正则表达式,而不是字符串,您需要删除其周围的引号。

 function isValidPostcode() { var p = document.getElementById('postcode').value.replace(/\\s/g, ''); var postcodeRegEx = /^([g][i][r][0][a][a])$|^((([a-pr-uwyz]{1}([0]|[1-9]\\d?))|([a-pr-uwyz]{1}[a-hk-y]{1}([0]|[1-9]\\d?))|([a-pr-uwyz]{1}[1-9][a-hjkps-uw]{1})|([a-pr-uwyz]{1}[a-hk-y]{1}[1-9][az]{1}))(\\d[abd-hjlnp-uw-z]{2})?)$/i; if (postcodeRegEx.test(p)) alert("OK"); else alert("This does not look a valid UK postcode..."); } document.getElementById("postcode").addEventListener("keyup", function() { isValidPostcode(); }); 
 <form name="register" method="post" action="" autocomplete="off"> <input id="postcode" type="text" name="postcode" class="form-control" placeholder="Postcode" maxlength="10" value='' required /> </form> 

you should replace onchange with keyup and remove quotes from regex :) 您应该用keyup替换onchange并从正则表达式中删除引号:)

<input type="text" name="postcode" class="form-control" placeholder="Postcode" maxlength="10"  value='' onkeyup="isValidPostcode(this.value)"  required />

function isValidPostcode(value) { 
        var postcodeRegEx = /^([g][i][r][0][a][a])$|^((([a-pr-uwyz]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[a-hk-y]{1}([0]|[1-9]\d?))|([a-pr-uwyz]{1}[1-9][a-hjkps-uw]{1})|([a-pr-uwyz]{1}[a-hk-y]{1}[1-9][a-z]{1}))(\d[abd-hjlnp-uw-z]{2})?)$/i; 
         if (postcodeRegEx.test(value)) console.log("OK"); 
         else console.log("This does not look a valid UK postcode...");
        }

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

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