douxigai8757 2014-12-03 00:53
浏览 48

如何在失败的javascript验证时运行php脚本?

I have a simple html signup form with onsubmit = "return validateForm()" where validateForm() is javascript to ensure input is in the correct form and e-mail and password fields match their confirm fields.

The forms action is set to add.php. add.php adds the input into a mysql database. Both the javascript and php work fine, however even when javascript alerts an error such as "password too short." the php script is run anyways and a new entry made in the database.

I would like the php script to only run upon successful javascript validation, and the user to remain on the same page if any validation fails, how is this done?

I can add code if needed but I think the answer here would be generic.

EDIT:

for example here is the code for validating name fields:

function checkName()
{
    var fName = document.getElementById("firstName");
    var lName = document.getElementById("lastName");
    if (fName.value.length >= 2 && fName.value.match(/^[A-Za-z]+$/) && lName.value.length >= 2 && lName.value.match(/^[A-Za-z]+$/))
        {
            return true;
        }
    else
        {
            return false;
        }
}

function validateForm(){
if (checkName() == false)
        {
            alert("First and Last Name must be at least 2 characters long and contain  only alphabetic characters");
        document.getElementById("fName").focus();
        document.getElementById("fName").select();
        return false;
    }

}

  • 写回答

1条回答 默认 最新

  • douxian9010 2014-12-03 01:09
    关注

    Is it firstName or fName? If the fName ID doesn't exist, the function will hit an error and exit without returning anything, causing the alert to show, but the form to still submit.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程