本文目錄一覽:
js表單驗證代碼怎麼寫
我來舉個例子,很簡單的
!DOCTYPE html
html
head
meta charset=”UTF-8″
titleInsert title here/title
script type=”text/javascript”
function formCheck(){//表單驗證
var userForm=document.forms.userForm;
if(userForm.username.value==null||userForm.username.value.length=0){
alert(“用戶名不能為空”);
return false;
}else if(userForm.username.value.length6||userForm.username.value.length20){
alert(“用戶名必須為6-20位”);
return false;
}else if(userForm.password.value==null||userForm.password.value.length=0){
alert(“密碼不能為空”);
return false;;
}else if(userForm.password.value.length6||userForm.password.value.length20){
alert(“密碼必須為6-20位”);
return false;
}else{
userForm.submit();
}
}
/script
/head
body
form action=”” name=”userForm”
userName:input type=”text” name=”username”br
password:input type=”password” name=”password”br
input type=”button” value=”提交” onclick=” formCheck()”
/form
/body
/html
如何用js提交struts2驗證表單?
s:submit type=”image” src=”images/index/register.gif” onclick=”return checkRegister();”/
改為:
img src=”images/index/register.gif” border=”0″ onclick=”return checkRegister();”
js中控制的時候驗證通不過的時候記得return false
例如:
if(username==””){
alert(‘用戶名不能為空’);
retrun false;
}
用JS提交表單時如何驗證表單項
都可以的,在以上的代碼裡面可以,在obsubmit代碼裡面也可以,例如可以:
function selectByMoreC(Action){
document.forms[“salChanceForm”].method = “post”;
document.forms[“salChanceForm”].action = Action;
if (document.forms[“salChanceForm”].myinput.value.match(/[^0-9]/))
{
alert(‘xxx只能輸入數字!’);
}//還可以添加其它一些欄位的判斷
else document.forms[“salChanceForm”].submit();
}
js註冊表單驗證代碼
JS函數:
function check(){
var username = document.getElementById(“username”).value;
var pass1 = document.getElementById(“password”).value;
var pass2 = document.getElementById(“confirm_password”).value;
if (username == “”|| username == NULL || pass1 == “” || pass1 == NULL || pass2 == NULL || pass2 == “”) {
alert(“用戶名或密碼不得為空!”);
return false;
}
}
表單提交的時候驗證:
input name=”reg” type=”submit” value=”提交” onclick=”return check()”/
自己補全。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129348.html