Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Struggling to validate that fields are not empty

Hello,

I run the following javascript when a form is submitting within my app. It is to check whether the required fields have been completed. It does not work... Whether data is entered or not in a field I always get the alert displayed. I'm an amateur so any help would be greatly appreciated.

---------------------------------

var Person = Apperyio('people_lookup').val();
var Brand = Apperyio('select_brand').val();
var Type = Apperyio('select_clothing').val();
var Size = Apperyio('enter_size').val();

var errorMsg = "";

if ($.trim(Person) === "");
{
errorMsg = errorMsg + "Person is a required field \n";
}

if ($.trim(Brand) === "");
{
errorMsg = errorMsg + "Brand is a required field \n";
}

if ($.trim(Type) === "");
{
errorMsg = errorMsg + "Type of clothing is a required field \n";
}

if ($.trim(Size) === "");
{
errorMsg = errorMsg + "Size is a required field \n";
}

if (errorMsg === "")
{ Create_Size_Record.execute ({});
Apperyio.navigateTo('MySizeIn', {transition : 'slideup'});
} else
{ alert ("Please correct the following issues: \n" +errorMsg);
}

____________________________________
Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Struggling to validate that fields are not empty

Hello Shaun,

Please write to console all values of variables to check them.

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Struggling to validate that fields are not empty

Hi,

Thanks for the reply. I wasn't sure how to do what you asked so I set up some local storage variable and wrote the value for the field to the storage variable when the submit button was clicked.

Also, I've chopped the code down to just one field for testing:

------------------------

var Size = Apperyio('enter_size').val();

var errorMsg = "";

if ($.trim(Size) === "");
{
errorMsg = errorMsg + "Size is a required field \n";
}

if (errorMsg === "")
{ Create_Size_Record.execute ({});
Apperyio.navigateTo('MySizeIn', {transition : 'slideup'});
} else
{ alert ("Please correct the following issues: \n" +errorMsg);
}

------------------------

Attached is a screen shot showing that 'enter_size' has a value completed (stored in local storage 'CheckData_Size.'

I don't understand therefore why the alert is displaying :(

Regards, Shaun

Image

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Struggling to validate that fields are not empty

You checked variable Size, which set from input component 'enter_size', not from localStorage. It is the reason of alert.

Can you add alert
alert(Size);

before check it?

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Struggling to validate that fields are not empty

Hi,

The localstorage was just so that I could confirm that the Size variable actually contained something (and was not blank).

I have done what you asked. See screen shot attached. The alert proves that the field is not blank hence why is the alert displaying?

Thanks, Shaun

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Struggling to validate that fields are not empty

Hi Shaun,

Please try following Js code:

pre

var Size = Apperyio('enter_size').val();

var errorMsg = "";

if (Size.trim() === "");
{
errorMsg = errorMsg + "Size is a required field \n";

Code: Select all

alert ("Please correct the following issues: \n" +errorMsg);  

}
else{
Create_Size_Record.execute ({});
Apperyio.navigateTo('MySizeIn', {transition : 'slideup'});
};

/pre

Regards.

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Struggling to validate that fields are not empty

Hi,

Thanks for your reply. I've tried your code but it doesn't work. In fact the screen containing the javascript won't even open when I use this code.

Regards, Shaun

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Struggling to validate that fields are not empty

Hi Shaun -

Please check if there are any errors in the browser console?

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Struggling to validate that fields are not empty

I've got it working but only when I check that it is not empty. I can't seem to get any variation to work for 'is empty':

var Size = Apperyio('enter_size').val();

Code: Select all

         if ($.trim(Size) !== "")              
         { Create_Size_Record.execute ({}); 

Apperyio.navigateTo('MySizeIn', {transition : 'slideup'});
} else {
alert("Please enter a Size");
}

Return to “Issues”