简体   繁体   English

我希望使我的代码验证一个字段,以确保它是四位数。 我怎样才能做到这一点?

[英]I wish to make my code validate a field to make sure it is a number of four digits. How can I do this?

I am currently doing a GCSE course which allows me to ask for assistance from IT sources as long as I reference them in my investigation. 我目前正在上一门GCSE课程,只要我在调查中引用了这些信息,我就可以从IT渠道寻求帮助。 I wish to make the following code validate that there is a number of four digits in the Examination Number entry field. 我希望使以下代码验证“考试编号”输入字段中是否有四个数字。 I'm okay with simple validation but this is another step for me. 我可以进行简单的验证,但这对我来说又是一步。

    <head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">

function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}

if (document.ExamEntry.examnumber.value=="") {
msg+="You must enter your Examination Number \n";
document.ExamEntry.examnumber.focus();
document.getElementById('examnumber').style.color="red";
result = false;
}

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
document.getElementById('examtype').style.color="red";
   }
}
if(checked==null)
{
    msg+="Please choose an option";
    result = false;
}
else{
     confirm("You have chosen "+checked.value+" is this the correct course?"); 
}

if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="examnumber">Examination Number</td>
<td><input type="text" name="examnumber" /></td>
</tr>
<tr>
<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br />
<td><input type="radio" id="examtype" name="examtype" value="AS"/> : AS<br />
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onClick="return validateForm();"    />  </td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>

Regarding checking if it is number, you can use some regex: eg 关于检查是否为数字,可以使用一些正则表达式:例如

var reg = /^\d{4}$/ig;
var regExRes = reg.exec(document.ExamEntry.examnumber.value);
if (document.ExamEntry.examnumber.value == "" || (regExRes == null || regExRes.length == 0)) {

    msg += "You must enter your Examination Number \n";
    document.ExamEntry.examnumber.focus();
    document.getElementById('examnumber').style.color = "red";
    result = false;
}

To avoid entering anything beside number, I would suggest you to use something like jquery pluging called masked input 为了避免在数字旁边输入任何内容,建议您使用类似jquery插入的方法,即“ masked input”

EDIT: line changed from var reg = /\\d{4}/ig; 编辑:var reg = /\\d{4}/ig;更改行var reg = /\\d{4}/ig; to var reg = /^\\d{4}$/ig; var reg = /^\\d{4}$/ig;

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

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