I am using AutoMailMerge and I am trying to add a JavaScript to check if a field value is "1" then insert image in the image field.
var f = this.getField("Image1_af_image");
if (f.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (f.value == "2")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}
This link contains 3 processed files with this above code with 3 diffrent values for "Image1_af_image".
https://gofile.io/d/qMvnfu
This could help ;)
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle"+ f.value+".png");
This worked now : )
var f = this.getField("Image1_af_image");
var n = this.getField("Number");
if (n.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (n.value == "2")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}
Related
I try to get rid of an ugly javscript eval method (Cause we all know it is unsecure).
I have the following problem. I build a dynamic searchstring.
Depends on the TLD a user decided to search for.
Here is my code:
if (tld == 0) {
var searchString = 'value.tld != ""';
}
if (tld == 1) {
var searchString = 'value.tld == "de"';
}
if (tld == 2) {
var searchString = 'value.tld == "com" || value.tld == "net" || value.tld == "org" || value.tld == "info" || value.tld == "biz"';
}
if (tld == 3) {
var searchString = 'value.tld == "io"';
}
Depending on the search parameter 'searchstring', I build this routine with eval:
if (eval(searchString)) {
// Do something special, depends on the tld variable
}
How can i rebuild this without using 'eval'. The premission is, that the first part of the code is beeing untouched.
Thanks in advance
Nick
How about:
let choices = {
1: ['de'],
2: ['com', 'net', 'org', 'info', 'biz'],
3: ['io']
};
function check(tldparam) {
if (tld === 0) {
return value.tld !== "";
} else {
return tld === tldparam && choices[tldparam].includes(value.tld);
}
}
And we test it like:
// Got this value from somewhere
let tld = 2;
let value = {tld: 'net'};
// This is my checking criterion
let tldparam = 2;
if (check(tldparam)) {
// Do something special, depends on the tld variable
}
Does it serve your purpose?
How can I find out if a text input is a certain text?
I tried this
<script>
var b = document.getElementById('button')
var u = document.getElementById('username')
var p = document.getElementById('password')
var bannedUsers = ["user1012"];
b.onclick = function() {
if(u.value.length <= 20 && p.value.length >= 6 && u.value.length >= 3 && !u.value === bannedUsers) {
location.href = "";
};
if(u.value.length > 20) {
return alert('Username needs to be below 20 characters.')
} else if(u.value.length < 3) {
return alert('Username needs to be above 2 characters')
}
if(p.value.length < 6) {
return alert('Password needs to be over 6 characters.')
}
if(u.value === bannedUsers) {
return alert('That username is banned.')
}
}
</script>
But it ended up just taking me to the page instead of saying "This username is banned"
You need to use the includes method.
bannedUsers.includes(u.value)
what you're doing right now is checking if the string is the array bannedUsers, translating to this: 'user1012' === '[object Object]'
You can use the Array.prototype.includes method to test if a given value is in an array. includes will return a boolean true or false.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
if (bannedUsers.includes(u.value) {
return alert('That username is banned.')
}
I'm trying to do some extremely simple form validation, my current problem is that my window.onload function doesn't call in the function I specify.
When I watch the flow of logic with firebug it just skips to the end of the code.
Here is an example of my code:
window.onload = init;
function init() {
var regForm = document.getElementById("registerform");
regForm.onsubmit = validatepostcode();
}
function validatepostcode() {
var postCode = document.getElementById("postcode");
var postCodeStr = postCode.charAt(0);
var state = document.getElementById("state");
var result = true;
if (postCodeStr == 3 || 8 && state == "Vic") {
result = true;
} else if (postCodeStr == (1 || 2) && state == "NSW") {
result = true;
} else if (postCodeStr == (4 || 9) && state == "QLD") {
result = true;
} else if (postCodeStr == 0 && state == "NT" || state == "ACT") {
result = true;
} else if (postCodeStr == 6 && state == "WA") {
result = true;
} else if (postCodeStr == 5 && state == "SA") {
result = true;
} else if (postCodeStr == 7 && state == "TAS") {
result = true;
} else
result = false;
if (result = false) {
alert("Your postcode does not match your state")
}
}
Five problems:
In init, you have this:
regForm.onsubmit = validatepostcode();
That calls validatepostcode and puts its return value in onsubmit. You probably meant to put the function itself it, not its return value in. Remove the parentheses:
regForm.onsubmit = validatepostcode;
In validatepostcode, you're fetching elements like this:
var postCode = document.getElementById("postcode");
…but then try to use them as values, like this:
var postCodeStr = postCode.charAt(0);
But an element and the current value of that element are not the same thing. More likely, you meant to retrieve the value on the first line:
var postCode = document.getElementById("postcode").value;
Same goes for state.
In validatepostcode, you have lines like this:
} else if (postCodeStr == (1 || 2) && state == "NSW") {
Specifically, 1 || 2 won't work like that. It will look at them like booleans and say, “one or two? well, they're both truthy…true it is!” and you'll essentially be doing
} else if (postCodeStr == true && state == "NSW") {
(Actually, it uses 1, not true, since the first operand was truthy, but that's not the important point here.)
Instead of using that abbreviated notation, you'll have to write it out longhand:
} else if ((postCodeStr == 1 || postCodeStr == 2) && state == "NSW") {
You mixed up = and == here:
if(result=false){
= will set result to false and leave the condition always false. Change it to == to test equality:
if(result==false){
You probably meant to return result at the end to prevent the form from being submitted when there is a validation error. With the other changes applied, you'd get an alert if there was a validation error, but it'd go on submitting anyway. As such, add a return result at the end of the validatepostcode function.
I found several solutions to this using jquery and javascript, however, it has to declare all the fields in jquery, but its not efficient since I have 30+ asp textboxes on my form, how can i prompt the user to save the data or changes have been made on page unload.
I use this:
Be sure to set the default="" attribute on your fields!
function formIsDirty(form) {
var i, j, element, type;
if(typeof form === 'undefined') {
return false;
}
for (i = 0; i < form.elements.length; i += 1) {
element = form.elements[i];
type = element.type;
if (type === "checkbox" || type === "radio") {
if (element.checked !== element.defaultChecked) {
return true;
}
} else if (type === "hidden" || type === "password" || type === "text" || type === "textarea") {
if (element.value !== element.defaultValue) {
return true;
}
} else if (type === "select-one" || type === "select-multiple") {
for (j = 0; j < element.options.length; j += 1) {
if (element.options[j].selected !== element.options[j].defaultSelected) {
return true;
}
}
}
}
return false;
}
$( window ).unload(function() {
if(formIsDirty(document.forms["formName"])) {
return confirm("There are unsaved changes, leave this page?");
}
}
As an extra tip, when you generate the page in asp, if you set the default="" attribute to the current value in the database, you can detect not only if it's empty, but if the value has been changed.
This also allows proper use of the form.reset() button/method (w3Schools page here
Assuming that all text text boxes have the same style class ('inputField' is used below) you can use something like:
$( window ).unload(function() {
if ($('.inputField').is(':empty')){
alert('Please fill all text boxes');
}
});
Im trying write JavaScript code which will generate a random number through a function then use that random number (between 1 and 6) to assign a value to the variable "background".
This is what I have:
function genBackground() {
var x=Math.floor((Math.random()*6)+1);
assignBG(x);
alert("test alert");
}
function assignBG(x) {
if (x === 1){
var background=blue;}
else if (x === 2){
var background=green;}
else if (x === 3){
var background=red;}
else if (x === 4){
var background=purple;}
else if (x === 5){
var background=yellow;}
else if (x === 6){
var background=orange;}
}
The alert "test alert" doesn't show, but does if but before the line "assignBG(x);". Have I done this bit wrong?
Are those "colors" defined previously in the code? Otherwise, you should write color = "white" and so on...
I'd also rewrite assignBG like this:
function genBackground() {
var x=Math.floor((Math.random()*6)+1);
var color = generateBG(x);
alert(color); // and do whatever you like with this color
}
function generateBG(x) {
var colors = ["blue","green","red","purple","yellow","orange"];
return colors[x];
}