HTML表单数据验证功能

发布于 2024-12-07 10:30:55 字数 3088 浏览 0 评论 0原文

我在 JavaScript 中创建了一个函数来验证 html 表单数据,我的代码如下:

function checkPetitionForm_ff() {
    if (document.petition_form.petition_firstname.value == "FIRST NAME" || document.petition_form.petition_firstname.value == "") {
        alert("Please enter your First Name!")
        document.petition_form.petition_firstname.focus();
        return false;
    }

    if (document.petition_form.petition_lastname.value == "LAST NAME" || document.petition_form.petition_lastname.value == "") {
        alert("Please enter your Last Name!")
        document.petition_form.petition_lastname.focus();
        return false;
    }

    if (document.petition_form.petition_age.value == "AGE" || document.petition_form.petition_age.value == "") {
        alert("Please enter your Age!")
        document.petition_form.petition_age.focus();
        return false;
    }

    if (document.petition_form.state.value == "Select State") {
        alert("Please select your state!")
        document.petition_form.state.focus();
        return false;
    }

    if (document.petition_form.petition_address.value == "HOME ADDRESS" || document.petition_form.petition_address.value == "") {
        alert("Please enter your address!")
        document.petition_form.petition_address.focus();
        return false;
    }

    if (document.petition_form.zip.value == "ZIP CODE" || document.petition_form.zip.value == "") {
        alert("Please enter your Zipcode!")
        document.petition_form.zip.focus();
        return false;
    }

    if (document.petition_form.phone2.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone2.focus();
        return false;
    }

    if (document.petition_form.phone1.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone1.focus();
        return false;
    }


    if (document.petition_form.phone3.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone3.focus();
        return false;
    }

    if (document.petition_form.level.value == "YOUR LEVEL OF EDUCATION") {
        alert("Please select your level of education!")
        document.petition_form.level.focus();
        return false;
    }

    if (document.petition_form.degree.value == "DEGREE OF INTEREST") {
        alert("Please select your degree!")
        document.petition_form.degree.focus();
        return false;
    }

    if (!(document.getElementById(edu).checked)) {
        alert("Please select  Education!")
        document.petition_form.edu.focus();
        return false;
    }


    else {
        return true;
    }

}

验证在“phone2”字段之前运行良好,在此之后将不会完成验证。

如果您能帮助我并建议如何解决此问题,我将不胜感激。

I've created a function in JavaScript to verify an html form data, my code as below:

function checkPetitionForm_ff() {
    if (document.petition_form.petition_firstname.value == "FIRST NAME" || document.petition_form.petition_firstname.value == "") {
        alert("Please enter your First Name!")
        document.petition_form.petition_firstname.focus();
        return false;
    }

    if (document.petition_form.petition_lastname.value == "LAST NAME" || document.petition_form.petition_lastname.value == "") {
        alert("Please enter your Last Name!")
        document.petition_form.petition_lastname.focus();
        return false;
    }

    if (document.petition_form.petition_age.value == "AGE" || document.petition_form.petition_age.value == "") {
        alert("Please enter your Age!")
        document.petition_form.petition_age.focus();
        return false;
    }

    if (document.petition_form.state.value == "Select State") {
        alert("Please select your state!")
        document.petition_form.state.focus();
        return false;
    }

    if (document.petition_form.petition_address.value == "HOME ADDRESS" || document.petition_form.petition_address.value == "") {
        alert("Please enter your address!")
        document.petition_form.petition_address.focus();
        return false;
    }

    if (document.petition_form.zip.value == "ZIP CODE" || document.petition_form.zip.value == "") {
        alert("Please enter your Zipcode!")
        document.petition_form.zip.focus();
        return false;
    }

    if (document.petition_form.phone2.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone2.focus();
        return false;
    }

    if (document.petition_form.phone1.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone1.focus();
        return false;
    }


    if (document.petition_form.phone3.value == "PHONE" || document.petition_form.phone1.value == "" || isNumeric(document.petition_form.phone1.value) == false) {
        alert("Please enter the complete phone No!")
        document.petition_form.phone3.focus();
        return false;
    }

    if (document.petition_form.level.value == "YOUR LEVEL OF EDUCATION") {
        alert("Please select your level of education!")
        document.petition_form.level.focus();
        return false;
    }

    if (document.petition_form.degree.value == "DEGREE OF INTEREST") {
        alert("Please select your degree!")
        document.petition_form.degree.focus();
        return false;
    }

    if (!(document.getElementById(edu).checked)) {
        alert("Please select  Education!")
        document.petition_form.edu.focus();
        return false;
    }


    else {
        return true;
    }

}

The verifications are working good until "phone2" field and will not complete the verification after this.

I'll do appreciate if you can help me and advise how to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

注定孤独终老 2024-12-14 10:30:55

我认为您遇到了异常,因为 isNumeric 不是 JavaScript 全局函数。您需要在页面中定义它(查看 在 JavaScript 中验证十进制数字 - IsNumeric() 以获得 <代码>isNumeric)。
此外,您还应该使用异常处理来包围您的方法调用,以获得更好的异常详细信息。

I think you are getting an exception as isNumeric is not a JavaScript Global function. You need to define it in your page (check out Validate decimal numbers in JavaScript - IsNumeric() for a clean implementation of isNumeric).
Also you should surround your method call with exception handling to get better details of the exception.

过度放纵 2024-12-14 10:30:55

在该行中,您实际上仅在第一个条件下检查 phone2,其他条件是 phone1

document.petition_form.phone2.value=="PHONE" || document.petition_form.phone1.value=="" || isNumeric(document.petition_form.phone1.value)==false

另请注意,您对 phone3 执行相同的操作。

In that line you're actually checking phone2 only in the first condition, the others are phone1.

document.petition_form.phone2.value=="PHONE" || document.petition_form.phone1.value=="" || isNumeric(document.petition_form.phone1.value)==false

Also be aware that you do the same for phone3.

音盲 2024-12-14 10:30:55

它看起来像是一个简单的复制/粘贴错误。请注意,phone2 之后引用的 petition_form 成员是 phone1...这没有意义。将此行与您的下一个验证进行比较,其中所有成员都是phone1。

所以,这一行:

      if (document.petition_form.phone2.value == "PHONE" || 
          document.petition_form.phone1.value == "" || 
isNumeric(document.petition_form.phone1.value) == false) {

应该看起来像:(

      if (document.petition_form.phone2.value == "PHONE" || 
          document.petition_form.phone2.value == "" || 
isNumeric(document.petition_form.phone2.value) == false) {

代码以这种方式排列以突出差异。)

It looks like a simple copy/paste error. Notice that the petition_form members referenced after phone2 are phone1... this does not make sense. Compare this line against your next validation where all of the members are phone1.

So, this line:

      if (document.petition_form.phone2.value == "PHONE" || 
          document.petition_form.phone1.value == "" || 
isNumeric(document.petition_form.phone1.value) == false) {

Should look like:

      if (document.petition_form.phone2.value == "PHONE" || 
          document.petition_form.phone2.value == "" || 
isNumeric(document.petition_form.phone2.value) == false) {

(Code is lined up in that manner to hilight the differences.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文