Javascript Form Validation with "z" && ( "x" || "y" ) - javascript

Seem to be having a little trouble getting some validation to work.
Currently trying to implement a system that will redirect a user if a variable is false, AND a form contains 0000 or 1111.
Example:
if xml_response == "false" && form == ("0000" || "1111") {
window.location.replace("");
} else {
submit.form
}
I've been able to get it working for 0000. If XML generates false, and form contains 0000, successful redirection.
If XML generates false and form contains 1111, no dice. I'm thinking it has to do with how I'm formatting my operators. Any ideas?
Here's my statement:
if (response == "false" && document.forms['Form'].id.value==("0000" || "1111") ) {
window.location.replace("http://url/");
} else {
document.getElementById("submit_form").submit();
}

That's invalid, you have to check for each value individually
if ((xml_response == "false") && (form == "0000" || form == "1111")) {
window.location.replace("");
} else {
submit.form
}
Note that you're checking for the string false and strings that look like numbers.
There's also the option to check an array for multiple values
if (
( response == "false" ) &&
( ['0000','1111'].indexOf(document.forms['Form'].id.value) != -1 )
)
{
window.location.replace("http://url/");
} else {
document.getElementById("submit_form").submit();
}
or even
/(0000|1111)/.exec(document.forms['Form'].id.value)

Note that:
document.forms['Form'].id.value
will attempt to find a form in the document with a name or ID of "Form". If it finds one, it will access it's ID property, which is a string, and attempt to read it's value property. Strings don't have a "value" property, so the result will be the value undefined.

Related

Is it possible to re-use a computed property by passing a parameter (Vuelidate)

I'm using Vuelidate for a form that has multiple sections. Each of these sections has formData object and name. Nested inside each of these is a ZIP object to validate zip code with numerous validations....required, numeric, minlength, and maxlength. What I would like to do is have ONE computed property zipCodeValid() and use on both. As of now I have two different computed properties targeting each section which is not terrible but I would like to see ONE reused for a cleaner approach.
Zip validation:
zip: {
required,
numeric,
minLength: minLength(5),
maxLength: maxLength(5),
}
computed: {
sectionOneZipValid() {
return (
(this.$v.formData.secOne.zip.$dirty &&
!this.$v.formData.secOne.zip.numeric) ||
(this.$v.formData.secOne.zip.$dirty &&
!this.$v.formData.secOne.zip.minLength) ||
(this.$v.formData.secOne.zip.$dirty &&
!this.$v.formData.secOne.zip.maxLength)
)
},
sectionTwoZipValid() {
return (
(this.$v.formData.secTwo.zip.$dirty &&
!this.$v.formData.secTwo.zip.numeric) ||
(this.$v.formData.secTwo.zip.$dirty &&
!this.$v.formData.secTwo.zip.minLength) ||
(this.$v.formData.secTwo.zip.$dirty &&
!this.$v.formData.secTwo.zip.maxLength)
)
}
}
Yes you can pass an argument like this..
computed: {
sectionZipValid() {
return sec => { return (
(this.$v.formData[sec].zip.$dirty &&
!this.$v.formData[sec].zip.numeric) ||
(this.$v.formData[sec].zip.$dirty &&
!this.$v.formData[sec].zip.minLength) ||
(this.$v.formData[sec].zip.$dirty &&
!this.$v.formData[sec].zip.maxLength)
)}
},
}
and it can be called as
sectionZipValid('secOne')
OR
sectionZipValid('secTwo')

Comparing problem with If else statement in Javascript

I have two condition of my same JSON data:
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[
{
"letterNo":13,
"letterId":1,
"inout":"out",
"inoutNo":"12332466544",
"inoutDate":"2070-01-01",
"letterIssuedSubBy":null,
"letterFile":null,
"representativeName":null,
"representativeNameEng":null,
"selectionId":124,
"assessmentNo":null,
"entryBy":"user98",
"rStatus":"I",
"others":null,
"signatary":"qwerrt",
"letterBox":null,
"imageFile":null,
"imageTitle":null,
"reminderYesNo":"N"
}
]
}
Same JSON with letter array empty structure :
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[]
}
All these values are stored in
var trDataSecondTable; . I tried to compare if the letter is empty or not by using if condition:
if(trDataSecondTable.letter != null) {
console.log("asasfasdfdsfdsfds");
$("#inoutDate").val(trDataSecondTable.letter[0].inoutDate);
$("#inoutNo").val(trDataSecondTable.letter[0].inoutNo);
$("#signatary").val(trDataSecondTable.letter[0].signatary);
}
else
{
console.log("entered in else part");
}
Though "letter":[] is empty it is not entering into else part. While comparing i also tried trDataSecondTable.letter.length != 0 but also it is not entering into else part.
Your condition should check for both null and the length:
if (trDataSecondTable.letter != null && trDataSecondTable.letter.length > 0)
Is is important to check for null before accessing the length property as it guarantees you won't try to access the length property on null, which would raise an exception. This is important since data is rarely reliable.
Because of the && operator, if the first check fails, the second check won't be evaluated.
Actually, an even safer way would be to check if the value is truthy, as this will work for null and undefined:
if (trDataSecondTable.letter && trDataSecondTable.letter.length > 0)
You could check the length of the array.
if (trDataSecondTable.letter.length) {
// letter has some elements
} else {
// no elements
}
I think this condition is enough
if(trDataSecondTable.letter.length)

Based on condition add true or false

I've 5 cells in a table. If the value are empty for those cells i need to disable them.
I can do something like this for each cells which work's.
function cellOne(params) {
if (params.value === null || params.value === undefined) {
return false
} else {
return true;
}
}
"CellOne": { disabled:cellOne }
is there any other way to check null value of each cell and add disable property instead of creating multiple function for each cells. Please help
You don't need to go for typescript code.You can do it in the template itself.
<your-cell [disabled]="!params.value"></your-cell>
Your function would return the same thing as :
function cellOne(params) {
return params.value != null
}
which you can easily inline instead of having a separate function for that.
To check for params that might be null or undefined too, you can use :
return params && params.value != null

SAPUI5 Formatter with visible form

could you help me for solve this issue ?
i have 2 or more field base on oData with different form. if i press with oData value (subty "1"), the result is true and oData (subty "2") is false.
then i press value "2", the result is true but value 1 is false
let' see my code :
in view, i write this code :
form 1
**
<f:SimpleForm visible="{path: 'Subty', formatter:'model.formatter.statusText'}"
/>
**
form 2
**
<f:SimpleForm visible="{path: 'Subty', formatter:'model.formatter.statusText1'}"
/>
*
in formatter, i write this code :
statusText: function (sStatus) {
if (sStatus==="1"){
return "true";}
if (sStatus==="2")
{return "false";}
else{
return "";
}
},
statusText1: function (sStatus) {
if (sStatus==="1"){
return "false";}
if (sStatus==="2")
{return "true";}
else{
return "";
}
}
i have error with that code.
Since the visible property is of type 'boolean' you will have to return a boolean value (true|false) in your formatter!
Please note:
"true" !== true
typeof "true" !== "boolean"
"false" !== false
typeof "false" !== "boolean"
typeof "" !== "boolean"
BR
Chris

Do you need to type out == null with Element.previousSibling?

If I want to check if an element is the first child of it's parent, I can do it like this:
if (element.previousSibling == null) {
...
}
But is it "wrong" to do it like this:
if (!element.previousSibling) {
...
}
? Since it still works since !!null == false and !!anyElement == true?

Categories