Javascript functions and return statements

  1. When to return null, when true, when false…

  2. Suppose i am having a simple form validation function

function validateEmail()
{
var emailID = document.myForm EMail.value;
atpos = emailID.indexOf("@ ");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
return( true );
}

why is it returning true and is inside a bracket…??

using valid syntax to retrieve the form value:

//invalid
var emailID = document.myForm EMail.value;

i think you need a dot between them:

var emailID = document.myForm.EMail.value;

But i will leave that for you to sort out. Why would you want to return null? I wouldn’t return Null, i would only use false or true (since the email is either false (invalid) or true (valid))

Why you not simply use a input type email is a mystery to me

1 Like

type email is inbuild so has some sort of limitations or bugs so decided to go with it i guess as i have read it somewhere…but my question is why does the function return true at the end and is inside brackets…

function validateEmail()
{
var emailID = document.myForm.EMail.value;
atpos = emailID.indexOf("@ “);
dotpos = emailID.lastIndexOf(”.");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert(“Please enter correct email ID”)
document.myForm.EMail.focus() ;
return false;
}
return( true );
}

yea, but for the fact that either way you need to server side validation of a email address the build in type email is pretty decent

You only need brackets for return when the return is multiply lines:

return (
  x
  + 
  1
)

which is not the case here, you can just remove the brackets

thanks a lot bro… :slight_smile:

Your welcome. Does your form work now? Just keep security in mind, javascript is loaded into the browser so it can be manipulated by the user, which means if you want to validate the email for something important (registering for a website), you need server side validation.

Now, i don’t know what you are intentions are, just as practice writing a email validation in javascript is good practice.

ya sure thanks…I’ll be using php for server side validation…will ping you if any help needed :smile:

my php skills are very rusty, i am using django (python) as backend

great…!! I wish I could master python… :unamused: