简体   繁体   English

表单验证出错(javascript)

[英]Error with form validation (javascript)

I have the following HTML code: 我有以下HTML代码:

<form name="Register" action="Register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile(FormName='Register');" >
  <p> Name* : <input id="FirstName" type="text" name="FirstName"/> </p>
<form>

And this JavaScript code: 这个JavaScript代码:

function isEmpty(field) {       
  if (field == "" || field == null)
    return false;
}

function validateProfile(FormName) {
    var errFname = "";
    var Fname = document.forms[FormName]["FirstName"].value;
    alert(isEmpty(field=Fname));
    return false;
}

The problem here is that the validation doesn't work... 这里的问题是验证不起作用......

I want that if the field is empty there will be an alert message. 我希望如果该字段为空,则会有一条警告消息。 The solution might be very simple, but I just start leran this validtion with javascript. 解决方案可能非常简单,但我只是开始使用javascript来验证此验证。

I have to used the isEmpty function in the code. 我必须在代码中使用isEmpty函数。

change 更改

validateProfile(FormName='Register');

to

validateProfile('Register');

and

alert(isEmpty(field=Fname));

to

   alert(isEmpty(Fname));


function validateProfile(FormName) {
    var errFname = "";
    var Fname = document.forms[FormName]["FirstName"].value;
    return isEmpty(Fname);
}

You should use the following code: 您应该使用以下代码:

function validateProfile(FormName) {
        var errFname = "";
        var Fname = document.getElementById("FirstName").value;
        alert(isEmpty(Fname));
        return false;
}

You can use the id of textfield as you have given it. 你可以使用你给出的文本字段的id。 And change the form tag with code: 并使用代码更改表单标记:

<form name="Register" action="Register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile('Register');" >

Javascript不允许按名称传递函数参数

alert(isEmpty(Fname)); // instead of isEmpty(field=Fname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM