如何在html网页中设置文本框为必填?

html-css023

如何在html网页中设置文本框为必填?,第1张

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>无标题文档</title>

<script type="text/javascript">

function a(){

var a=document.getElementById("a").value

if(a==""){

alert("必填项,请重新输入")

return false

}

}

</script>

</head>

<body>

<textarea id="a" cols="60" rows="3" ></textarea>

<input type="submit" property="tokenNote" value="submit" onclick="a()" />

</body>

</html>

我给你上传到一个简单的网页,用的JS,你参考一下吧,

最简单的就是js验证,如下,验证文本内容非空. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8"><title>title</title><script type="text/javascript">function chk(){ var text = document.form1.text1.valueif(text){ alert('empty string')} } </script></head><body><form name="form1"><input type="text" name="text1" /><input type="button" value="Submit" onclick="return chk()" /></form></body></html>

麻烦采纳,谢谢!