使用JavaScript在onblur和按submit时验证表单
我正在尝试使用JavaScript验证表单中的字段。当用户离开字段(onblur)或当用户按下submit时,应验证字段。如果对必填字段的验证以任何方式失败,则不应发送表单 问题是我还有一个JS函数,如果验证成功,它应该重写一个被验证的字段,并发送表单 这是我的HTML:使用JavaScript在onblur和按submit时验证表单,javascript,html,forms,validation,onblur,Javascript,Html,Forms,Validation,Onblur,我正在尝试使用JavaScript验证表单中的字段。当用户离开字段(onblur)或当用户按下submit时,应验证字段。如果对必填字段的验证以任何方式失败,则不应发送表单 问题是我还有一个JS函数,如果验证成功,它应该重写一个被验证的字段,并发送表单 这是我的HTML: <head> <meta charset='UTF-8'> <script type="text/javascript" src="./library/checkcreateus
<head>
<meta charset='UTF-8'>
<script type="text/javascript" src="./library/checkcreateuser.js"></script>
<script type="text/javascript" src="./library/hashcreateuser.js"></script>
</head>
<body>
<div class="maindiv">
<form name="createform" id="createform" onsubmit="return formhash();" action="#" method="post">
<input type="text" name="email" id="email" onblur="checkEmail()" placeholder="E-postadress" maxlength="50" />
<label for="email" id="labemail"></label><br />
<input type="text" name="testemail" id="testemail" onblur="checkEmailConfirm()" placeholder="Bekräfta e-postadress" maxlength="50" /><br />
<label for="testemail" id="labtestemail"></label><br />
<br />
... other input fields that should be validated, not yet written ...
<br />
<input type="password" name="password" id="password" placeholder="Lösenord" maxlength="50" /><br />
<label for="password" id="labpassword"></label><br />
<input type="password" name="testpassword" id="testpassword" placeholder="Bekräfta lösenord" maxlength="50" /><br />
<label for="testpassword" id="labtestpassword"></label><br />
<br />
<input type="submit" placeholder="Registrera" onclick="validateForm()"><br />
</form>
</div>
</body>
要检查在电子邮件和testemail中输入的值,应使用:
var email = document.getElementById("email").value;
var testemail = document.getElementById("testemail").value;// then use split on these values.
如果你愿意
var email = document.getElementById("email");//you will get error may be like split is not a function or something similar.
我将把这个放在这里考虑:-使用jQuery一切都会更好。至于这个问题,您是否在javascript在调试器中运行的过程中仔细检查了它?这通常是任何javascript调试过程中的第一步。@JamesGaunt问题是我从未使用过jQuery,很少使用JS,而且我没有时间真正学习jQuery,因为今晚我必须完成这项工作。至于调试,不,我甚至没有想到。感谢您提供的提示。代码复查,
validateForm
是一个很好的阅读工具,为什么不是一行:return(checkEmail()&&checkEmailConfirm())代码>@Sukima没有想到,谢谢:)
var email = document.getElementById("email").value;
var testemail = document.getElementById("testemail").value;// then use split on these values.
var email = document.getElementById("email");//you will get error may be like split is not a function or something similar.