-1

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.

1

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This won't change much - will it?
It isn't smart to do it but I can't see why it should cause errors
Hi, Thank you! But the problem now is that even if the field is empty after submit the form is submiting, how can I preven this?
can u explain clearly.What is after submit the form is submiting,
After click submit, I see the alert, but after closing the the alert, the form is submiting, even that the field is empty...
|
1

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');" >

Comments

0

Javascript does not allow passing function parameters by name

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.