AddPatient = {};
AddPatient.Firstname = FirstNameValue || PatientModel.errorMsg('FirstName',FirstNameValue);
AddPatient.LastName = LastNameValue || PatientModel.errorMsg('LastName',LastNameValue);
AddPatient is an Object and i am checking it whether its blank or not before sending the request.
PatientModel.js
errorMsg: function(title,FirstNameValue,LastNameValue) {
if(FirstNameValue === undefined || FirstNameValue === ' ' && LastNameValue === undefined || LastNameValue = ' ') {
alert('FirstName and LastName are missing');
return false;
} else {
alert(+title 'is missing');
return false;
}
}
I have a form, where i have FirstName and LastName field and i have to check it should not be blank. I want a single function in javascript which can work.
Is this the right way to do it?
I can see a couple of problems in your code.
errorMsg()‘s expected arguments and how it is calledalert()ifstatementMismatch between
errorMsg()‘s expected arguments and how it is calledYour
errorMsg()function expects three arguments, but you only pass it two at a time:If you really want to use both values inside
errorMsg(), you need to pass them both every time, in the same order the function expects them:Syntax error in second
alert()This is simple enough to fix, and could have been just a typo.
What you want is this:
Bad expression inside
ifstatementThis won’t work as you expect, because
&&has greater precedence than||, meaning the expression will be evaluated as such:You would need parenthesis to fix the precedence:
This is irrelevant, actually, because the expression can be simplified like this:
Or even better, like this:
How I would fix your code
This would be an incomplete answer if I didn’t provide a correct version of your code, so here it goes. Notice that I separate filling-in of information and its validation in two steps.
Edit: a different, perhaps better, approach. The only difference is we now write validate() as a method of a Patient object: