Page 1 of 1

Struggling to validate that fields are not empty

Posted: Mon May 25, 2015 11:34 am
by Shaun Summers

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);
}

____________________________________

Struggling to validate that fields are not empty

Posted: Tue May 26, 2015 7:06 am
by Serhii Kulibaba

Hello Shaun,

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


Struggling to validate that fields are not empty

Posted: Tue May 26, 2015 11:32 am
by Shaun Summers

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


Struggling to validate that fields are not empty

Posted: Wed May 27, 2015 11:02 am
by Serhii Kulibaba

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?


Struggling to validate that fields are not empty

Posted: Wed May 27, 2015 1:54 pm
by Shaun Summers

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


Struggling to validate that fields are not empty

Posted: Mon Jun 01, 2015 2:43 am
by Yurii Orishchuk

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.


Struggling to validate that fields are not empty

Posted: Mon Jun 08, 2015 8:12 am
by Shaun Summers

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


Struggling to validate that fields are not empty

Posted: Mon Jun 08, 2015 8:55 am
by Illya Stepanov

Hi Shaun -

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


Struggling to validate that fields are not empty

Posted: Mon Jun 08, 2015 9:26 am
by Shaun Summers

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");
}