Javascript Validation is not validating right - javascript

I am trying to build simple shopping cart where the user input books information and price, the javascript file would validate the input then after inputting several books info, the user would be able to view the order with books details and total price. The problem is that even if there was an error with the dataentry, the wrong information would be stored and hence I get a NaN for the total price.

In function Validate() you start off with var correct = 'true' and later have an if statement checking if it is still true, however there is no code before that if which could possibly change var correct to a value other than true. I'm guessing you forgot to add correct = 'false' in each of the ifs you have checking the variables.
Also, if you want var correct to be a boolean rather than a string, get rid of the "" around true.

var Price=document.getElementById('Price').value;
if(isNaN(Price)||Price="")
{alert("Please enter numbers only for price");
correct = false;
}

It's never a good idea to perform client side navigation. What if someone has javascript disabled? (Blackberry phones). They will pass your validation, and boom, everything messes up.
As for your question, it would be easier to answer if you could have an isolated test case in a fiddle.
EDIT: As others said, it's simply because you aren't doing correct = false every time in the if statements.

Related

Assigning form values in Angular 9

I have a challenge and I need help.
I am trying to integrate a payment system and I got some stuck, I have something like this:
options: PaystackOptions= {
amount: 5000,
email: 'user#email.com',
ref: `'some code that generate random stuff'`
Some methods other.
Now when I use hard coded values it works fine but doesn't work when I attempts to assign values from my form. My question is how do I assign values from my reactive form to the above for it to work. When I console log the form values , there are values.
I try assigning values like this in my onSubmit method where the Payments is still initiated that is I have one button where I called the onSubmit method and Initiated the Payment.
this.options.amount = this.donateForm.value['amount'];
this.options.email = this.donateForm.value['email'];
When I console log this.options.amount , the value is there but somehow doesn't get passed to options when I assign it, I know this is not a better explanation for my problem but any help would be appreciated.
Assuming your requirement, see if the following help to fix your issue
https://stackblitz.com/edit/angular-ivy-sdkh8q

Condition Issue NetSuite Email Merge from SuiteScript With Template Using Freemarker

I'm working on some functionality that sends out notification emails. There may or may not be a contact specified. So when I initially set up the mail merge, this is what I'm doing:
var template = nlapiCreateEmailMerger(_TEMPLATE_ID);
template.setEntity("customer", customerId);
if (!isEmpty(contactID)) {
template.setEntity("contact", contactID);
}
Because there may or may not be a contact specified, in my email template I have Freemarker in place to check to see if a contact is there. If so, it adds their first and last name. If not, it adds the "companyname" value from the customer record. This is what I have:
<#if contact.id?has_content>${contact.firstName} ${contact.lastName}<#else>${customer.companyName}</#if>
The issue that I'm having is that when a contact is added, it displays the contact name just fine. However, if no contact is added, the conditional statement fails completely. Nothing shows up at all, even if "companyname" has a value. I've tried the following, but the conditional statement always resolves to false:
<#if contact?has_content>${contact.firstName} ${contact.lastName}<#else>${customer.companyName}</#if>
I've tried seeing what's going on, and if I place all of the values outside of the conditional statement, I get values when I expect them, and blank space when I expect it. So I know there is no issue with the data itself, as far as I can tell.
I know I can get around this by creating two separate templates and picking the correct one in the script, but that becomes trouble when having to make changes and I'd prefer to only have one template to maintain.
So, I suppose the real question is, what is the appropriate value to use in the condition statement that works whether or not a contact record is added?
Ok, I had an epiphany and solved the issue. In hindsight, it was pretty obvious. Here's what I did.
I basically created a variable, and set it to be the "companyname" value. Then, if there was a contact, assign it the contact's name. Here's the code:
<#assign greeting = customer.companyName>
<#if contact.id?has_content><#assign greeting = contact.firstName + " " + contact.lastName></#if>
${greeting},<br />
I hope this helps someone else.

How can I handle a potentially pre-filled form with Jade?

I have this huge form that only gets pre-filled data if data already exists in a database. Otherwise, none of the text boxes should have the value parameter in them. I thought it would just ignore the value parameters if the variable I get data from does not exist. But instead, I get an error.
How to I handle this case? Will I have to do an if check before each text box in Jade like the following?
if (typeof(prefilled_data) !== 'undefined')
input.form-control#prevalence(type="text", name="prevalence")
else
input.form-control#prevalence(type="text", name="prevalence", value=prefilled_data.tb_burden_estimates.prevalence)
While I don't mind doing this (Sublime Text will help with all the copy-pasting), there are quite a few form fields, so it might get ugly. Is there a way to consolidate this into one check somewhere?
you seemed to be suggesting that the if statement's were going to be bulky/make the code hard to read if they were there.. my suggestion would be to programmatically create the inputs, there by reducing the if statements to a more manageable number and answering your question about being able to "consolidate this into one check somewhere"
EDIT
If you are looking to access data in js.. I have been known to use something like:
script(type='text/javascript').
window.prefilled_data = !{ JSON.stringify(prefilled_data) };
This will allow you then to access the global window.prefilled_data variable to get front end version of your data
you can do this:
- if (typeof(prefilled_data) === 'undefined'){
- prefilled_data = '';
- }
input.form-control#prevalence(type="text", value=#{prefilled_data})
if prefilled_data is undefined you just set a '' value

Why won't my function follow my if/else conditions?

Here's the fiddle: http://jsfiddle.net/K5dsh/
I'm trying to make a simple calculator that evaluates the difference between the numbers its given, and gives an answer based on that. It has two problems:
1.) The answer my script gives is always the result of my 2nd else if statement, even if the input matches the conditions before it.
2.) The answer does not change even when there's a new input that should give a different result.
Does anyone see what the problems are? Thank you.
As Ivan pointed out, you're never updating the values of high, low and common with the values entered into the textboxes. You should be assigning the values inside the calculate function. See updated fiddle.
Also, you may want to invest some time into learning a framework like Knockout.js. It makes data-binding HTML form elements to JavaScript view models incredibly simple.
Your variables are defined only once. You should change it so that every time your button is clicked, it grabs the new variables. Here is an example: http://jsfiddle.net/Vd8n4/
Your values aren't being updated.
Here's how I would go about it. I'd change your function so that it starts off like this. That way, every time it is called, it updates the high, low, and common values.
function calculate() {
var high = document.getElementById('highRi').value;
var low = document.getElementById('lowRi').value;
var common = document.getElementById('comm').value;

Get Titanium.App.Properties in another win or make it global available

Please don’t answer this if you don’t take the time to understand my question or have a reasonable answer. I have got a few answers that is far on the side and I think I explain my problem very clear. Shall this problem drive me nuts or is there somebody out there with a straight and clear answer on Titanium.App.Properties?
I have a login form that stores the username in one:
Titanium.App.Properties.setString("userName",e.value);
and the password in another:
Titanium.App.Properties.setString("passWord",e.value);
The forms TextFields holds these values(after a store) even if I close the window, shut down and restarts the app. This because of the App.Properties.getString("value"); I suppose….?!
But when I copy the hole form with its App.Properties.getString("value"); to another window, the TextFields are empty and contains no value at all. I understand that the Properties string must be there some where in App.Properties, but how can grab it and use it another place in the app?
My question is: How to get the value from my
var userNameValue = Titanium.App.Properties.getString("userNameValue");
to be available in another window or for the hole app(global)? How can I grab it and make use of it a another place in the app? I don’t see a good reason to make these, only separate words, into objects(JSON) since the userName only contains a e-mail address and the passWord consist only of continuing letters or numbers. But if you mean I have too, -how do I set this from my TextField and get it in another TextField somewhere else in my app. I have not had any luck so far. I hope you can help me out and I can keep sane.
Titanium.App.Properties.getString("userNameValue");
This is globally Available, any Propertie of the Titanium Object is accessible in each file.
but if for some reason this doesnt work for you and you want to set a global variable,
you could do the following:
Create a file called myGlobals.js //or anything else,
//Put this in there e.g
var myProperties = {};
in any file you want to use it write in the first line
Ti.include('myGlobals.js');
Then you can make a propertie global available, for example write this in app.js somewhere where the app initializes
myProperties.Username = Titanium.App.Properties.getString("userName");
Then you can get the value in each file by accesing the propertie
myProperties.Username
//of course the Propertie has to be set before you can get them
( Titanium.App.Properties.setString("userName",e.value); ) //like you do it
But, Titanium.App.Properties.getString("userName");
should be avilable from any file anyway, (but you can give this a try although i dont think its nice to do it like this)
i had a similar problem where this didnt get any value from a propertie set in the ios settings as default value.
I had to go to the settings and manually change or edit the default value and then after a restart
Titanium.App.Properties.getString("userName");
returned the value as it should,
i hope this helps you =)
Answer to the Comment
I'm glad i could help you =)
Yes you can Use an focus EventHandler like this :
textfield.addEventListener("focus",function(){
textfield.value = "Test";
});
Beside that , are you using the identical Textfield for both windows ? like
var Textfield = Ti.Ui.createTextField({...});
and add it to 2 different windows ?
win1.add(Textfield);
win2.add(Textfield);
That led for me to Problems with labels in TableViewRows, using an identical Row 2 times in the TableView
The Text displayed only on 1 Label, sometimes it switched the Labels
I think you can't add one and the same titanium object to more then one other object
Maybe that could be the culprit,
dunno just an idea =)

Categories