简体   繁体   English

HTML和JavaScript验证

[英]HTML and Javascript validation

I am trying to add a function, or additional JavaScript to the existing function for a confirmation, I need to insert an additional number field to the form to take the users examination number, the examination number is 4 digits and the JavaScript form will need to validate that it is 4 digits I am new to this and any help is much appreciated. 我正在尝试向现有功能添加功能或其他JavaScript进行确认,我需要在表格中插入一个额外的数字字段以获取用户的考试编号,考试编号为4位数字,并且JavaScript表格需要确认这是4位数字,我对此是新手,我们非常感谢您的帮助。

here is my code so far 到目前为止,这是我的代码

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <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(msg==" "){
return result;
}
{
alert(msg)
return result;
            }


}
</script>
}
</style>
</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>
            <tr>
                            <td id="subject ">Subject</td>
                            <td><input type="text " name="subject " /></td>
            </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>

You can use RegExp ^[0-9]{1,4}$ to test the value that matches 4 digits and can type only numbers 0-9 您可以使用RegExp ^[0-9]{1,4}$测试与4位数字匹配的值,并且只能输入数字0-9

Include this in your javascript function 将此包含在您的javascript函数中

var inputVal=document.ExamEntry.name.value;

var patt = new RegExp("^[0-9]{1,4}$");
var res = patt.test(inputVal);
if(!res)
{
  alert("Please enter a valid 4 digit number");
}

简单的HTML很好

<input type="text" name="pin" maxlength="4" size="4">

Code will be look like 代码看起来像

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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;
}
var patt = new RegExp("^[0-9]{1,4}$");

if (!patt.test(document.ExamEntry.examNumber.value)) {
            msg+="You must enter 4 digit examination number \n";
            document.ExamEntry.subject.focus();
            document.getElementById('examNumber').style.color="red";
            result = false;
}

return result;
            }

function onSubmit(){
  if(confirm('Are you sure to submit')){
  return validateForm();
  }
return false;
}

</script>
</style>
</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>
            <tr>
                            <td id="subject">Subject</td>
                            <td><input type="text" name="subject" /></td>
            </tr>

            <tr>
                            <td><input type="submit" name="Submit" value="Submit"        
onclick="return onSubmit();" /></td>
                            <td><input type="reset" name="Reset" value="Reset" /></td>
            </tr>
</table>
</form>
</body>

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

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