简体   繁体   中英

Error with form validation (javascript)

I have the following HTML code:

<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:

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.

I have to used the isEmpty function in the code.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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