English 中文(简体)
如何使用 JAVASCRIPT 检查 HTML 表格中的输入, 并当场打印错误信息?
原标题:How to check input from HTML form and print ERROR message on the spot using JAVASCRIPT?
<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
 document.write("Enter a unique username");
 return false;
 }
if (p!=rp){
 document.write("Retype Password Incorrect");
 return false;
 }
return true;
}
</script>

信件印在单独的页面上, 但是我要在文本框前的同一个地方打印! 请帮助 。 谢谢!

最佳回答

永远不要使用文档写入, 它很危险...

文档“getEplementById”(“硫丹元素化于该页面”) 。 InnerHTML = “用 javascript 添加的字符串 ” ;

<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
  document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Enter a unique username";
 return false;
 }
if (p!=rp){
 document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Retype Password Incorrect";
 return false;
 }
return true;
}
</script>
问题回答

文档. write 将仅将信件附加到页面的末尾。最简单的解决方案是将其更改为这样的提醒 :

if (u==""){
 alert("Enter a unique username");
 return false;
}

否则您需要创建新的 DOM 元素( 手头之前或发现错误时), 以保持信件并放置在输入处旁边

您需要检测到文本更改。 一种方法是使用 :

<input name="blah" onchange="validate()" ... />

EDIT To add text on the same page, include the script using <script src=...> and then add <div id= validation[i] ></div> next to each of the inputs where [i] is increased by 1 each time (so validation1 validation2 so on). Then, use getElementById( validation[i] ).innerHtml to change the value.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签