25

I am doing a client side form validation to check if passwords match. But the validation function always returns undefined.

function validatePassword(errorMessage) { var password = document.getElementById("password"); var confirm_password = document.getElementById("password_confirm"); if(password.value) { // Check if confirm_password matches if(password.value != confirm_password.value) { return false; } } else { // If password is empty but confirm password is not if(confirm_password.value) { return false; } } return true; } 

Please note that the validatePassword is called from a member function of the Form object.

function Form(validation_fn) { // Do other stuff this.submit_btn = document.getElementById("submit"); this.validation_fn = validation_fn; } Form.prototype.submit = funciton() { var result; if(this.validation_fn) { result = this.validation_fn(); } //result is always undefined if(result) { //do other stuff } } 
6
  • This function will never return undefined. Please define "is called from a member function of the Form object." Commented Oct 14, 2011 at 15:50
  • just hit CTRL+SHIFT+J -- check if error If yes post here , If not then open debugger and run it . Commented Oct 14, 2011 at 16:10
  • @AnilShanbhag Does not return any error. I did open the debugger and when I step through the code, is when I realised that result is being set as undefined Commented Oct 14, 2011 at 16:27
  • You're excluding some relevant information from your question. I would guess either that somewhere in there you're making a server request (or doing some other asynchronous action), or when you create the new Form() object, you're either not passing a function, or you're trying to pass one that doesn't yet exist. Have you verified that validatePassword is even being invoked? Please provide all relevant code. There's nothing here that demonstrates an issue. Commented Oct 14, 2011 at 17:20
  • 1
    ok I found it.. When I created a new Form Object, it should've been new Form(function() {return eval(objs.validator);}) instead of new Form(function() {eval(objs.validator);}),, missed a return statement in there.. Thanks guys for helping Commented Oct 14, 2011 at 22:14

5 Answers 5

18

You could simplify this a lot:

  • Check whether one is not empty
  • Check whether they are equal

This will result in this, which will always return a boolean. Your function also should always return a boolean, but you can see it does a little better if you simplify your code:

function validatePassword() { var password = document.getElementById("password"); var confirm_password = document.getElementById("password_confirm"); return password.value !== "" && password.value === confirm_password.value; // not empty and equal } 
Sign up to request clarification or add additional context in comments.

8 Comments

@primvdb That still returns undedined.. I am doing something bizzare
@tryurbest: Are you getting any errors perhaps? This function always returns, and what it returns is a boolean... And in your code, then this.validation_fn should be false. What is the value of this.validation_fn?
@pimvdb.. AS you can see I am assigning the value of this.validation_fn to result right.. When I step via the debugger, result is undefined.
@tryurbest: You're only doing so if(this.validation_fn). So perhaps that variable is undefined somehow. Just check to make sure.
@primvdb that is just to check if there is a validation_fn or not in the Form object.. If there is, call the function with result = this.validation()
|
8

You could wrap your return value in the Boolean function

Boolean([return value]) 

That'll ensure all falsey values are false and truthy statements are true.

1 Comment

btw u can also try return !!somevalue
2

Don't forget to use var/let while declaring any variable.See below examples for JS compiler behaviour.

function func(){ return true; } isBool = func(); console.log(typeof (isBool)); // output - string let isBool = func(); console.log(typeof (isBool)); // output - boolean 

Comments

2

An old thread, sure, but a popular one apparently. It's 2020 now and none of these answers have addressed the issue of unreadable code. @pimvdb's answer takes up less lines, but it's also pretty complicated to follow. For easier debugging and better readability, I should suggest refactoring the OP's code to something like this, and adopting an early return pattern, as this is likely the main reason you were unsure of why the were getting undefined:

function validatePassword() { const password = document.getElementById("password"); const confirm_password = document.getElementById("password_confirm"); if (password.value.length === 0) { return false; } if (password.value !== confirm_password.value) { return false; } return true; } 

1 Comment

If the functions only purpose is to return a boolean then the function name should conform to a naming conventions such as isValidPassword
-1

If we don't return anything explicitly, Javascript functions implicitly returns 'undefined'. In the below code, one condition is not returning anything, which is causing it to return undefined. I have mentioned this in the code comments.

function validatePassword(errorMessage) { var password = document.getElementById("password"); var confirm_password = document.getElementById("password_confirm"); if(password.value) { // Check if confirm_password matches if(password.value != confirm_password.value) { return false; } //Here a return statement is missing } else { if(confirm_password.value) { return false; } // Here a return statement is missing } return true; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.