【问题标题】:How to validate form using several functions in javascript如何使用 javascript 中的多个函数验证表单
【发布时间】:2015-05-11 07:54:21
【问题描述】:

这两个功能完美运行,但我一次只能使用一个,表单应检查密码并根据所选单选按钮将用户链接到其他页面。有没有办法让它发挥作用??

验证密码的功能。

function check() 
{ 
   var password=document.getElementById('password');
   var confirm=document.getElementById('confirm'); 

   if(password==confirm) 
   { 
       return true; 
   } 
   else 
   { 
      alert('Password do not match'); 
      return false; 
   } 

} 

功能2:根据选择将用户引导到2个不同的html页面

function doSubmit(form) 
{ 
   var urls = form['url']; 
   var i = urls && urls.length; 
   while (i--) 
   { 
     if (urls[i].checked) 
     { 
       window.location = urls[i].value; 
     } 
   } 
     return false; 
}

这是html代码(我只包含了与这些功能相关的部分)

<form action="" onsubmit="return check()">


   <label for="password">Password</label>
    <input type="password" id="password" maxlength="20" size="10" required="true"    pattern=".{7,}" title="Password should be atleast 7 characters long"/>

   <br /><br />

  <label for="confirm">Confirm Password</label>
   <input type="password" id="confirm"" maxlength="20" size="10" required="true"   />
    <label for="mcq">MCQ</label>
   Beginner<input type="radio" name="url" value="begin" id="rdbegin"  value="beginner.html"required="true"/>
   <br />
  Expert<input type="radio" name="url" value="expert" id="rdexpert" value="expert.html"required="true"/>
  <br /><br />

  <button type="submit" id="submit">Submit</button>
</form>

【问题讨论】:

  • 您是否尝试从check() 内部调用doSubmit()
  • 是的,我也试过了,我在 if else 语句之后调用了该函数,我什至将它们插入到包装函数中但它不起作用,基本上 check() 工作正常,我能够验证密码,但 doSubmit() 不起作用,我点击提交后页面刚刚刷新。

标签: javascript validation field


【解决方案1】:

编辑:

试试这个来获取单选按钮的值:

function doSubmit(form) 
{ 
     var urls = form['url']; 
     console.log(urls.value);
     window.location = urls.value;
}

也可以使用.value 来获取密码值:

var password=document.getElementById('password').value;

否则以下条件将永远为假:

if(password==confirm) // because you were comparing two html nodes

这是working DEMO


验证后,您可以在第一个函数中简单地调用第二个函数,如下所示:

function check() 
{ 
   var password=document.getElementById('password');
   var confirm=document.getElementById('confirm'); 

   if(password==confirm) 
   { 
       var form = document.getElementById('form'); //give id="form" to your form
       doSubmit(form);
       return true; 
   } 
   else 
   { 
      alert('Password do not match'); 
      return false; 
   } 

} 

对于onSubmit() 函数,请避免使用whileloop,这将导致无限循环,因为i-- 将始终为真,您只需这样做:

var urls = form['url']; 
for(var i=0; urls[i]; ++i){
   if(urls[i].checked){
         console.log(urls[i].value); //try printing the url and see what it gets
         window.location = urls[i].value; // also change this to urls[i]
         break;
   }
}

【讨论】:

  • 我尝试了你的建议,但我遇到了同样的问题,表单只是在提交时刷新,我只有当我在表单中包含 doSubmit() 时才会遇到这个问题,如果我单独使用它会指导我到我指定的页面。我也尝试在表单标签中调用 doSubmit 只是为了查看它是否有效,但我收到错误“找不到页面”
  • 你能把html代码贴出来,让我们看看网址吗?
  • 初学者
    专家

  • 不,我的意思是整个 html 代码,尝试将 url 打印到控制台,请参阅我的编辑。
  • 好的,请稍等我试图在这里粘贴代码,
猜你喜欢
  • 2017-07-22
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
相关资源
最近更新 更多