If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Can't get equal to null to work

Could someone please tell me why this doesn't work before I pull my hair out? I am trying to validate a simple form with JavaScript and I want to check if the form fields are null but can't get it to work if they are. If I change the statement to != and put something in the form field, it will prompt the alert. Shouldn't it work the same way if it is null? I even tried equal to an empty string (=== "") and that didn't work either.

Oh. I would rather use innerHTML instead of an alert box for the error message. Is there a way to get the innerHTML to stay on the page. I realize that when the submit button is clicked, the page reloads and that is why it goes away, but is there a way to stop it from reloading?

So here is the problem but I am not sure how to do it properly. When the submit button is pressed, the values in the form fields are not being passed into the variables. Instead, [object HTMLInputElement] is what is being passed into the variables.

We replace all characters which are not spaces, tabulations, ... before to test.

Hm...if user "inputs" one or more empty strings (by hitting his/hers spacebar key") the check will fail and I'm sure you don't want to allow empty strings as user names. Maybe something like this would work better:

Code:

var re = /^\B|\b {2,}/

that would prevent empty strings and two or more empty spaces... For example: "" or "name surname" (note the double space between name and surname).

Could someone please tell me why this doesn't work before I pull my hair out? I am trying to validate a simple form with JavaScript and I want to check if the form fields are null but can't get it to work if they are. If I change the statement to != and put something in the form field, it will prompt the alert. Shouldn't it work the same way if it is null? I even tried equal to an empty string (=== "") and that didn't work either.

Oh. I would rather use innerHTML instead of an alert box for the error message. Is there a way to get the innerHTML to stay on the page. I realize that when the submit button is clicked, the page reloads and that is why it goes away, but is there a way to stop it from reloading?

Thanks to anyone taking a look!

First you're initialing the userName variable with the <input> element (object) instead of its value.

Code:

//this line of code should be corrected
var usrName = document.getElementById('rUsrName');
//with this line of code
var usrName = document.getElementById('rUsrName').value

Second null is a word of special meaning in JavaScript. You cannot check if userName is equal to null. You can check if its length property is equal to 0 which would indicate that there were no input at all or you can check it for empty strings or some other value...

Be aware that if you're going to check the length of a rUsrName field, space characters are also counting. So, one solution to this problem is to use Regular expression to test for valid inputs.

Here is what I ended up writing. I am not sure if it makes much of a difference but I changed the onclick that was at the submit button to an onsubmit at the form tag.
I found the way to solve the problem of the page reloading each time was to return false if the check fails. That way, the page does not reload and I can make the innerHRML error message stay.