在Javascript Validation页面重定向到另一个页面之后

时间:2014-11-03 17:11:10

标签: javascript

我尝试使用javascript验证文本框。当我点击提交按钮而不将值插入文本框的显示警告框。但是点击"确定"按钮,页面重定向到payments.php页面。修复它

<form method="post" action="payments.php"  >
                First Name : <br />
                 <input name="name" type="text" class="ed" id="name"  /> <br />

                E-mail : <br />
                <input name="email" type="text" class="ed" id="email"  />  <br />


                <input name="but" type="submit" value="Confirm" onclick="Validation()" />
            </form>


function Validation()
{

if (checkFName()&&checkemail())
    {

   window.event.returnValue = false;
     }
}

function checkFName()
     {
var tname = document.getElementById("name").value;
if((tname == null)||(tname == ""))
{
   alert("Please Enter your First name");
return false;
}
return true;
}


 function checkemail()
  {
  var email = document.getElementById("email").value;
  var ap = email.indexOf("@");
  var dp = email.lastIndexOf(".");
  if((ap < 1)||(dp-ap < 1)||(dp >= email.length-1))
  {
  alert("invalid email address");
  return false;
  }
  else
  return true;
  }

1 个答案:

答案 0 :(得分:0)

您需要将CONFIRM按钮类型更改为&#34;按钮&#34;:

<input name="but" type="button" value="Confirm" onclick="Validation()" /> 

为表单命名:

<form name="frm" method="post" action="payments.php">

然后在Validation()函数中,如果表单通过验证,则可以执行以下操作:

function Validation()
{
    if (checkFName()&&checkemail())
    {
        document.forms["frm"].submit();
    }
 }