Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6022721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:53:57+00:00 2026-05-23T03:53:57+00:00

AddPatient = {}; AddPatient.Firstname = FirstNameValue || PatientModel.errorMsg(‘FirstName’,FirstNameValue); AddPatient.LastName = LastNameValue || PatientModel.errorMsg(‘LastName’,LastNameValue); AddPatient

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:53:58+00:00Added an answer on May 23, 2026 at 3:53 am

    I can see a couple of problems in your code.

    • Mismatch between errorMsg()‘s expected arguments and how it is called
    • Syntax error in second alert()
    • Bad expression inside if statement

    Mismatch between errorMsg()‘s expected arguments and how it is called

    Your errorMsg() function expects three arguments, but you only pass it two at a time:

    errorMsg: function(title,FirstNameValue,LastNameValue) {
        ....
    }
    
    ... and then ....
    
    .errorMsg('FirstName',FirstNameValue);
    .errorMsg('FirstName',LastNameValue);
    

    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:

    PatientModel.errorMsg('FirstName',FirstNameValue,LastNameValue);
    PatientModel.errorMsg('LastName',FirstNameValue,LastNameValue);
    // This is weird code, but it'll work
    

    Syntax error in second alert()

    This is simple enough to fix, and could have been just a typo.

    alert(+title 'is missing');
          ^      ^_There's something missing here.
          |_This will only try to convert title to a number
    

    What you want is this:

    alert(title + 'is missing');
    

    Bad expression inside if statement

    if(FirstNameValue === undefined || FirstNameValue === ' ' && LastNameValue === undefined || LastNameValue = ' ') {
    

    This won’t work as you expect, because && has greater precedence than ||, meaning the expression will be evaluated as such:

    if (
         FirstNameValue === undefined
     || (FirstNameValue === ' ' && LastNameValue === undefined)
     ||  LastNameValue = ' '
    ) {
    

    You would need parenthesis to fix the precedence:

    if( (FirstNameValue === undefined || FirstNameValue === ' ') && (LastNameValue === undefined || LastNameValue = ' ') ) {
    

    This is irrelevant, actually, because the expression can be simplified like this:

    // If these values are taken from a form input, they are guaranteed to be strings.
    if(FirstNameValue.length === 0 && LastNameValue.length === 0) {
    

    Or even better, like this:

    // Uses regular expressions to checks if string is not whitespace only
    var WHITESPACE = /^\s*$/;
    if( WHITESPACE.test(FirstNameValue) && WHITESPACE.test(FirstNameValue)){
    

    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.

    PatientModel.js :
    
    validate: function(patient){
        var WHITESPACE = /^\s*$/;
        var errors = [];
    
        if( WHITESPACE.test(patient.FirstName) ){
            // No first name
            if( WHITESPACE.test(patient.LastName) ){
                // No last name either
                errors.push('FirstName and LastName are missing');
            }
            else {
                // Only first name missing
                errors.push('FirstName is missing');
            }
        }
        else if( WHITESPACE.test( patient.LastName) ){
            // Only last name missing
            errors.push('LastName is missing');
        }
    
        // Returns array of errors
        return errors;
    }
    
    
    
    Your other code:
    
    AddPatient = {};
    AddPatient.Firstname = FirstNameValue; 
    AddPatient.LastName = LastNameValue;
    
    errors = PatientModel.validate(AddPatient);
    if( errors.length != 0 ){
        alert('You have the following errors:\n' + errors.join('\n'));
    }
    

    Edit: a different, perhaps better, approach. The only difference is we now write validate() as a method of a Patient object:

    >>> PatientModel.js:
    
    var WHITESPACE = /^\s*$/;
    
    // Creates a new empty Patient
    function Patient(){
        this.FirstName = '';
        this.LastName = '';
    }
    
    // Adds a validate() method to all Patient instances
    Patient.prototype.validate: function(){
        var errors = [];
    
        if( WHITESPACE.test(this.FirstName) ){
            // No first name
            if( WHITESPACE.test(this.LastName) ){
                // No last name either
                errors.push('FirstName and LastName are missing');
            }
            else {
                // Only first name missing
                errors.push('FirstName is missing');
            }
        }
        else if( WHITESPACE.test( thisLastName) ){
            // Only last name missing
            errors.push('LastName is missing');
        }
    
        // Returns array of errors
        return errors;
    }
    
    
    
    
    >>> Your other code :
    
    patient = new Patient();
    patient.FirstName = FirstNameValue;
    patient.LastName = LastNameValue;
    
    errors = patient.validate();
    if( errors.length != 0 ){
        alert('You have the following errors:\n' + errors.join('\n'));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's a synonym for a many-to-many relationship? I've finished writing an object-relational mapper but
Lets say I have a custom collection and a custom object that have a
I have a xml file like this: <Patients> <patient name=someName lastName=someLastName> <aProperty>anIntegerValue</aProperty> </patient> </Patients>
hanks everybody to help my idiot problem (look my before post:) ) . But
I am learning EF Code First from Programming Entity Framework Code First. The following
i have a datatable i created below i need to list all rows' cell
How can i solve below string join error. i converted int value to string
I have defined a C#-class, that shall be the elements of a directed graph
Is it normal when you submit an In-App Purchase, that it causes applicationWillResignActive while
I'm trying to figure out how I can listen to the Cancel button that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.