Javascript required fields validation - javascript

I have a big form where I have to validate 44 fields. I'm using javascript form validation for requiring field input, and C# regular expressions for ensuring valid input. I won't go into details on why I had to do it this way, but that's how it works. I've triple checked this code block I have to validate the form and can't find the error. Code is below (large), hoping someone has better eyes than me.
C#
#using (Html.BeginForm("Create", "Table1_1", FormMethod.Post, new { onsubmit = "return validateit()", name = "myForm" }))
{
Javascript
<script>
function validateit() {
var x1 = document.forms["myForm"]["COMPANY1"].value;
var x2 = document.forms["myForm"]["STATE1"].value;
var x3 = document.forms["myForm"]["BILL_CODE1"].value;
var x4 = document.forms["myForm"]["EFFECTIVE_DATE1"].value;
var x5 = document.forms["myForm"]["RECORD_TYPE1"].value;
var x6 = document.forms["myForm"]["MASK_ID1"].value;
var x7 = document.forms["myForm"]["EXTENSION_ID1"].value;
var x8 = document.forms["myForm"]["OVERPAYMENT_LIMIT"].value;
var x9 = document.forms["myForm"]["UNDERPAYMENT_LIMIT"].value;
var x10 = document.forms["myForm"]["OVERPAYMENT_REFUND"].value;
var x11 = document.forms["myForm"]["PARTIAL_PAYMENT_REFUND"].value;
var x12 = document.forms["myForm"]["RETURN_PREMIUM_REFUND"].value;
var x13 = document.forms["myForm"]["CANCELLATION_REFUND"].value;
var x14 = document.forms["myForm"]["EFT_AUTH_CHECK"].value;
var x15 = document.forms["myForm"]["EFT_CHECK_BILL"].value;
var x16 = document.forms["myForm"]["EFT_NSF_LIMIT"].value;
var x17 = document.forms["myForm"]["DB_PREM_ROUND"].value;
var x18 = document.forms["myForm"]["CC_OPTION"].value;
var x19 = document.forms["myForm"]["NSF_CHECK_LIMIT"].value;
var x20 = document.forms["myForm"]["NSF_CHECK_OPTION"].value;
var x21 = document.forms["myForm"]["FIRST_TERM_TYPE"].value;
var x22 = document.forms["myForm"]["CARRY_DATE_OPTION"].value;
var x23 = document.forms["myForm"]["DB_DPAY_ENDORSE_DAYS"].value;
var x24 = document.forms["myForm"]["DATE_METHOD"].value;
var x25 = document.forms["myForm"]["RENEWAL_OPTION"].value;
var x26 = document.forms["myForm"]["DROP_DAYS"].value;
var x27 = document.forms["myForm"]["MULTI_PAY_IND"].value;
var x28 = document.forms["myForm"]["MINIMUM_PAYMENT"].value;
var x29 = document.forms["myForm"]["ENDORSEMENT_ACTION"].value;
var x30 = document.forms["myForm"]["I_AND_S_OPTION_DAYS"].value;
var x31 = document.forms["myForm"]["S_OPTION_PERCENT"].value;
var x32 = document.forms["myForm"]["SERVICE_CHARGE_OPTION"].value;
var x33 = document.forms["myForm"]["REINSTATE_OPTION"].value;
var x34 = document.forms["myForm"]["CASH_WITH_APP_OPTION"].value;
var x35 = document.forms["myForm"]["DB_CC_NOTICE"].value;
var x36 = document.forms["myForm"]["DOWN_PAY_RENEWAL_DAYS"].value;
var x37 = document.forms["myForm"]["MONTH_BY_TERM"].value;
var x38 = document.forms["myForm"]["LEAD_MONTHS"].value;
var x39 = document.forms["myForm"]["INITIAL_MONTHS"].value;
var x40 = document.forms["myForm"]["DB_CC_REJECTS"].value;
var x41 = document.forms["myForm"]["RETURN_ENDORSEMENT_OPTION"].value;
var x42 = document.forms["myForm"]["RTN_S_OPTION_PERCENT"].value;
var x43 = document.forms["myForm"]["DB_AUTO_REF_DAY"].value;
var x44 = document.forms["myForm"]["RENEW_OPTION_BILL_PLAN"].value;
var count = 0;
if (x1 == null || x1 == "" || x1 == " ") {
document.getElementById("demo1").innerHTML = "<font style='color:red';>Error: Company number is required</font>";
count = count + 1;
}
else {
document.getElementById("demo1").innerHTML = null;
}
if (x2 == null || x2 == "" || x2 == " ") {
document.getElementById("demo2").innerHTML = "<font style='color:red';>Error: State code is required</font>";
count = count + 1;
}
else {
document.getElementById("demo2").innerHTML = null;
}
if (x3 == null || x3 == "" || x3 == " ") {
document.getElementById("demo3").innerHTML = "<font style='color:red';>Error: Bill code is required</font>";
count = count + 1;
}
else {
document.getElementById("demo3").innerHTML = null;
}
if (x4 == null || x4 == "" || x4 == " ") {
document.getElementById("demo4").innerHTML = "<font style='color:red';>Error: Effective date is required</font>";
count = count + 1;
}
else {
document.getElementById("demo4").innerHTML = null;
}
if (x5 == null || x5 == "" || x5 == " ") {
document.getElementById("demo5").innerHTML = "<font style='color:red';>Error: Record type is required</font>";
count = count + 1;
}
else {
document.getElementById("demo5").innerHTML = null;
}
if (x6 == null || x6 == "" || x6 == " ") {
document.getElementById("demo6").innerHTML = "<font style='color:red';>Error: Mask id is required</font>";
count = count + 1;
}
else {
document.getElementById("demo6").innerHTML = null;
}
if (x7 == null || x7 == "" || x7 == " ") {
document.getElementById("demo7").innerHTML = "<font style='color:red';>Error: Extension id is required</font>";
count = count + 1;
}
else {
document.getElementById("demo7").innerHTML = null;
}
if (x8 == null || x8 == "" || x8 == " ") {
document.getElementById("demo8").innerHTML = "<font style='color:red';>Error: Overpayment limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo8").innerHTML = null;
}
if (x9 == null || x9 == "" || x9 == " ") {
document.getElementById("demo9").innerHTML = "<font style='color:red';>Error: Underpayment limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo9").innerHTML = null;
}
if (x10 == null || x10 == "" || x10 == " ") {
document.getElementById("demo10").innerHTML = "<font style='color:red';>Error: Overpayment refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo10").innerHTML = null;
}
if (x11 == null || x12 == "" || x11 == " ") {
document.getElementById("demo11").innerHTML = "<font style='color:red';>Error: Partial payment refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo11").innerHTML = null;
}
if (x12 == null || x12 == "" || x12 == " ") {
document.getElementById("demo12").innerHTML = "<font style='color:red';>Error: Return premium refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo12").innerHTML = null;
}
if (x13 == null || x13 == "" || x13 == " ") {
document.getElementById("demo13").innerHTML = "<font style='color:red';>Error: Cancellation refund is required</font>";
count = count + 1;
}
else {
document.getElementById("demo13").innerHTML = null;
}
if (x14 == null || x14 == "" || x14 == " ") {
document.getElementById("demo14").innerHTML = "<font style='color:red';>Error: EFT auth check</font>";
count = count + 1;
}
else {
document.getElementById("demo14").innerHTML = null;
}
if (x15 == null || x15 == "" || x15 == " ") {
document.getElementById("demo15").innerHTML = "<font style='color:red';>Error: EFT check bill is required</font>";
count = count + 1;
}
else {
document.getElementById("demo15").innerHTML = null;
}
if (x16 == null || x16 == "" || x16 == " ") {
document.getElementById("demo16").innerHTML = "<font style='color:red';>Error: EFT NSF limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo16").innerHTML = null;
}
if (x17 == null || x17 == "" || x17 == " ") {
document.getElementById("demo17").innerHTML = "<font style='color:red';>Error: Premium rounding is required</font>";
count = count + 1;
}
else {
document.getElementById("demo17").innerHTML = null;
}
if (x18 == null || x18 == "" || x18 == " ") {
document.getElementById("demo18").innerHTML = "<font style='color:red';>Error: Credit card option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo18").innerHTML = null;
}
if (x19 == null || x19 == "" || x19 == " ") {
document.getElementById("demo19").innerHTML = "<font style='color:red';>Error: NSF check limit is required</font>";
count = count + 1;
}
else {
document.getElementById("demo19").innerHTML = null;
}
if (x20 == null || x20 == "" || x20 == " ") {
document.getElementById("demo20").innerHTML = "<font style='color:red';>Error: NSF check option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo20").innerHTML = null;
}
if (x21 == null || x21 == "" || x21 == " ") {
document.getElementById("demo21").innerHTML = "<font style='color:red';>Error: First term type is required</font>";
count = count + 1;
}
else {
document.getElementById("demo21").innerHTML = null;
}
if (x22 == null || x22 == "" || x22 == " ") {
document.getElementById("demo22").innerHTML = "<font style='color:red';>Error: Carry date option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo22").innerHTML = null;
}
if (x23 == null || x23 == "" || x23 == " ") {
document.getElementById("demo23").innerHTML = "<font style='color:red';>Error: Down pay endorse days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo23").innerHTML = null;
}
if (x24 == null || x24 == "" || x24 == " ") {
document.getElementById("demo24").innerHTML = "<font style='color:red';>Error: Date method is required</font>";
count = count + 1;
}
else {
document.getElementById("demo24").innerHTML = null;
}
if (x25 == null || x25 == "" || x25 == " ") {
document.getElementById("demo25").innerHTML = "<font style='color:red';>Error: Renewal option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo25").innerHTML = null;
}
if (x26 == null || x26 == "" || x26 == " ") {
document.getElementById("demo26").innerHTML = "<font style='color:red';>Error: Drop days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo26").innerHTML = null;
}
if (x27 = null || x27 == "" || x27 == " ") {
document.getElementById("demo27").innerHTML = "<font style='color:red';>Error: Multipay ind is required</font>";
count = count + 1;
}
else {
document.getElementById("demo27").innerHTML = null;
}
if (x28 == null || x28 == "" || x28 == " ") {
document.getElementById("demo28").innerHTML = "<font style='color:red';>Error: Minimum payment is required</font>";
count = count + 1;
}
else {
document.getElementById("demo28").innerHTML = null;
}
if (x29 == null || x29 == "" || x29 == " ") {
document.getElementById("demo29").innerHTML = "<font style='color:red';>Error: Endorsement action is required</font>";
count = count + 1;
}
else {
document.getElementById("demo29").innerHTML = null;
}
if (x30 == null || x30 == "" || x30 == " ") {
document.getElementById("demo30").innerHTML = "<font style='color:red';>Error: I and S option days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo30").innerHTML = null;
}
if (x31 == null || x31 == "" || x31 == " ") {
document.getElementById("demo31").innerHTML = "<font style='color:red';>Error: S option percent is required</font>";
count = count + 1;
}
else {
document.getElementById("demo31").innerHTML = null;
}
if (x32 == null || x32 == "" || x32 == " ") {
document.getElementById("demo32").innerHTML = "<font style='color:red';>Error: Service charge option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo32").innerHTML = null;
}
if (x33 == null || x33 == "" || x33 == " ") {
document.getElementById("demo33").innerHTML = "<font style='color:red';>Error: Reinstate option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo33").innerHTML = null;
}
if (x34 == null || x34 == "" || x34 == " ") {
document.getElementById("demo34").innerHTML = "<font style='color:red';>Error: Cash with application is required</font>";
count = count + 1;
}
else {
document.getElementById("demo34").innerHTML = null;
}
if (x35 == null || x35 == "" || x35 == " ") {
document.getElementById("demo35").innerHTML = "<font style='color:red';>Error: Credit card notice is required</font>";
count = count + 1;
}
else {
document.getElementById("demo35").innerHTML = null;
}
if (x36 == null || x36 == "" || x36 == " ") {
document.getElementById("demo36").innerHTML = "<font style='color:red';>Error: Down pay renewal days is required</font>";
count = count + 1;
}
else {
document.getElementById("demo36").innerHTML = null;
}
if (x37 == null || x37 == "" || x37 == " ") {
document.getElementById("demo37").innerHTML = "<font style='color:red';>Error: Month by term is required</font>";
count = count + 1;
}
else {
document.getElementById("demo37").innerHTML = null;
}
if (x38 == null || x38 == "" || x38 == " ") {
document.getElementById("demo38").innerHTML = "<font style='color:red';>Error: Lead months is required</font>";
count = count + 1;
}
else {
document.getElementById("demo38").innerHTML = null;
}
if (x39 == null || x39 == "" || x39 == " ") {
document.getElementById("demo39").innerHTML = "<font style='color:red';>Error: Initial months is required</font>";
count = count + 1;
}
else {
document.getElementById("demo39").innerHTML = null;
}
if (x40 == null || x40 == "" || x40 == " ") {
document.getElementById("demo40").innerHTML = "<font style='color:red';>Error: Credit card rejects is required</font>";
count = count + 1;
}
else {
document.getElementById("demo40").innerHTML = null;
}
if (x41 == null || x41 == "" || x41 == " ") {
document.getElementById("demo41").innerHTML = "<font style='color:red';>Error: Return endorsement option is required</font>";
count = count + 1;
}
else {
document.getElementById("demo41").innerHTML = null;
}
if (x42 == null || x42 == "" || x42 == " ") {
document.getElementById("demo42").innerHTML = "<font style='color:red';>Error: Return S option percent is required</font>";
count = count + 1;
}
else {
document.getElementById("demo42").innerHTML = null;
}
if (x43 == null || x43 == "" || x43 == " ") {
document.getElementById("demo43").innerHTML = "<font style='color:red';>Error: Auto ref day is required</font>";
count = count + 1;
}
else {
document.getElementById("demo43").innerHTML = null;
}
if (x44 == null || x44 == "" || x44 == " ") {
document.getElementById("demo44").innerHTML = "<font style='color:red';>Error: Renew option bill plan is required</font>";
count = count + 1;
}
else {
document.getElementById("demo44").innerHTML = null;
}
if (count > 0) {
return false;
}
}
</script>
Note: this code works in all of my other views. I must have a typo somewhere but I just can't seem to find it. thanks!

The x27 check is assigning to null instead of testing for null:
if (x27 = null || x27 == "" || x27 == " ") {
Should be
if (x27 == null || x27 == "" || x27 == " ") {
Although you might find something like this easier to work with if you really do need this function:
function validateit() {
var fields = [
["COMPANY1", "Company number is required"],
["STATE1", "State code is required"],
["BILL_CODE1", "BILL_CODE1 is required"],
["EFFECTIVE_DATE1", "EFFECTIVE_DATE1 is required"],
["RECORD_TYPE1", "RECORD_TYPE1 is required"],
// ...
];
var count = 0;
for (var i = 0; i < fields.length; i++) {
var x = document.forms.myForm[fields[i][0]].value;
if (x === null || x === "" || x === " ") {
count++;
document.getElementById("demo" + (i + 1)).innerHTML = "<font style='color:red';>Error: " + fields[i][1] + "</font>";
}
else {
document.getElementById("demo" + (i + 1)).innerHTML = "";
}
}
}

Related

How to check a varying range of indexes in JavaScript

I have an array that contains some data. Each block of data is separated by an empty line, and the size of the block varies. How would I go about checking each block to see if it contained the values I need. Here is my current code that handles this. (This doesn't work because it will always check 7 lines)
input = ["ecl:gry pid:860033327 eyr:2020 hcl:#fffffd",
"byr:1937 iyr:2017 cid:147 hgt:183cm",
"",
"iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884",
"hcl:#cfa07d byr:1929",
"",
"hcl:#ae17e1 iyr:2013",
"eyr:2024",
"ecl:brn pid:760753108 byr:1931",
"hgt:179cm",
"",
"hcl:#cfa07d eyr:2025 pid:166559648",
"iyr:2011 ecl:brn hgt:59in"];
var byr = false;
var iyr = false;
var eyr = false;
var hgt = false;
var hcl = false;
var ecl = false;
var pid = false;
var numberOfLines = 1;
if (input[arrayIndex+1].charAt(0) == ""){
numberOfLines = 1;
} else if (input[arrayIndex+2].charAt(0) == ""){
numberOfLines = 2;
} else if (input[arrayIndex+3].charAt(0) == ""){
numberOfLines = 3;
} else if (input[arrayIndex+3].charAt(0) == ""){
numberOfLines = 3;
} else if (input[arrayIndex+4].charAt(0) == ""){
numberOfLines = 4;
} else if (input[arrayIndex+5].charAt(0) == ""){
numberOfLines = 5;
} else if (input[arrayIndex+6].charAt(0) == ""){
numberOfLines = 6;
}
else if (input[arrayIndex+7].charAt(0) == ""){
numberOfLines = 7;
}
if (input[arrayIndex].includes("byr:") || input[arrayIndex + 1].includes("byr:") || input[arrayIndex + 2].includes("byr:") || input[arrayIndex + 3].includes("byr:") || input[arrayIndex + 4].includes("byr:") || input[arrayIndex + 5].includes("byr:") || input[arrayIndex + 6].includes("byr:")) {
byr = true;
} else { byr = false; }
if (input[arrayIndex].includes("iyr") || input[arrayIndex + 1].includes("iyr:") || input[arrayIndex + 2].includes("iyr:") || input[arrayIndex + 3].includes("iyr:") || input[arrayIndex + 4].includes("iyr:") || input[arrayIndex + 5].includes("iyr:") || input[arrayIndex + 6].includes("iyr:")) {
iyr = true;
} else { iyr = false; }
if (input[arrayIndex].includes("eyr:") || input[arrayIndex + 1].includes("eyr:") || input[arrayIndex + 2].includes("eyr:") || input[arrayIndex + 3].includes("eyr:") || input[arrayIndex + 4].includes("eyr:") || input[arrayIndex + 5].includes("eyr:") || input[arrayIndex + 6].includes("eyr:")) {
eyr = true;
} else { eyr = false; }
if (input[arrayIndex].includes("hgt:") || input[arrayIndex + 1].includes("hgt:") || input[arrayIndex + 2].includes("hgt:") || input[arrayIndex + 3].includes("hgt:") || input[arrayIndex + 4].includes("hgt:") || input[arrayIndex + 5].includes("hgt:") || input[arrayIndex + 6].includes("hgt:")) {
hgt = true;
} else { hgt = false; }
if (input[arrayIndex].includes("hcl:") || input[arrayIndex + 1].includes("hcl:") || input[arrayIndex + 2].includes("hcl:") || input[arrayIndex + 3].includes("hcl:") || input[arrayIndex + 4].includes("hcl:") || input[arrayIndex + 5].includes("hcl:") || input[arrayIndex + 6].includes("hcl:")) {
hcl = true;
} else { hcl = false; }
if (input[arrayIndex].includes("ecl:") || input[arrayIndex + 1].includes("ecl:") || input[arrayIndex + 2].includes("ecl:") || input[arrayIndex + 3].includes("ecl:") || input[arrayIndex + 4].includes("ecl:") || input[arrayIndex + 5].includes("ecl:") || input[arrayIndex + 6].includes("ecl:")) {
ecl = true;
} else { ecl = false; }
if (input[arrayIndex].includes("pid:") || input[arrayIndex + 1].includes("pid:") || input[arrayIndex + 2].includes("pid:") || input[arrayIndex + 3].includes("pid:") || input[arrayIndex + 4].includes("pid:") || input[arrayIndex + 5].includes("pid:") || input[arrayIndex + 6].includes("pid:")) {
pid = true;
} else { pid = false; }
if (byr && iyr && eyr && hgt && hcl && ecl && pid) {
console.log("True");
} else {
console.log("False");
}
Using reduce to create an object with all your fields might be easier:
const inputStr = `ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in`;
const input = inputStr.split('\n\n'); // Split on empty lines
const fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
input.forEach(passport => {
const res = fields.reduce((res, field) => ({
...res, // Keep the previous fields
[field]: passport.includes(field) // Add the current one
}), {});
console.log(JSON.stringify(res)); // {"byr": true, "iyr": true, ...}
const allFieldsArePresent = Object.values(res).every(val => val);
console.log(allFieldsArePresent);
});
When writing code, always try to think "DRY" (Don't Repeat Yourself). If you find yourself repeating things over and over, it's a big code-smell, there is a 99.999% chance it could be written in a shorter, more maintainable way.

Creating an array that tells if a given number is smaller than 10

I am trying to make this work, I don't know what is wrong with it.
It used to work with only 3 variables, when I made it longer it stoped and now I cannot make it work even with only 3. Now it gives me "Undefined Undefined Undefined".
What I want is to crate an alarm in a way that it shows the name of whatever has gone under 10. The fields it is comparing from are calculation fields with the REPEATABLESUM() code.
var choice1 = '';
var choice2 = '';
var choice3 = '';
var choice4 = '';
var choice5 = '';
var choice6 = '';
var choice7 = '';
var choice8 = '';
var choice9 = '';
var choice10 = '';
var choice11 = '';
var choice12 = '';
var choice13 = '';
var choice14 = '';
var choice15 = '';
var choice16 = '';
var choice17 = '';
var choice18 = '';
var choice19 = '';
var choice20 = '';
var choice21 = '';
var choice22 = '';
var choice23 = '';
var choice24 = '';
if (NUM($stock_coliblue_24) < 10) {
choice1 = LABEL('coliblue');
}
if (NUM($stock_absorvent_pads) < 10) {
choice2 = LABEL('absorvent_pads');
}
if (NUM($stock_micro_filters) < 10) {
choice3 = LABEL('micro_filters');
}
if (NUM($stock_alkaphotalkalinity) < 10) {
choice4 = LABEL('alkaphot');
}
if (NUM($stock_ammonia) < 10) {
choice5 = LABEL('ammonia');
}
if (NUM($stock_calcicol_calcium_hardness) < 10) {
choice6 = LABEL('calcicol');
}
if (NUM($stock_chloridol_nacl) < 10) {
choice7 = LABEL('chloridol');
}
if (NUM($stock_free_chlorine_dpd1) < 10) {
choice8 = LABEL('chlorine_free_dpd1');
}
if (NUM($stock_total_chlorine_dpd3) < 10) {
choice9 = LABEL('chlorine_total_dpd2');
}
if (NUM($stock_free_copper_n1) < 10) {
choice10 = LABEL('copper_free_n1');
}
if (NUM($stock_total_copper_n2) < 10) {
choice11 = LABEL('copper_total_n2');
}
if (NUM($stock_fluoride) < 10) {
choice12 = LABEL('fluoride');
}
if (NUM($stock_hardicol_total_hardness) < 10) {
choice13 = LABEL('hardicol');
}
if (NUM($stock_iron_hr) < 10) {
choice14 = LABEL('iron_hr');
}
if (NUM($stock_iron_mr) < 10) {
choice15 = LABEL('iron_mr');
}
if (NUM($stock_magnecol_magnesium) < 10) {
choice16 = LABEL('magnecol');
}
if (NUM($stock_manganese) < 10) {
choice17 = LABEL('manganese');
}
if (NUM($stock_nitratest_nitrate) < 10) {
choice18 = LABEL('nitratest');
}
if (NUM($stock_nitricol_nitrite) < 10) {
choice19 = LABEL('nitricol');
}
if (NUM($stock_phosphate) < 10) {
choice20 = LABEL('phosphate');
}
if (NUM($stock_potassium) < 10) {
choice20 = LABEL('potassium');
}
if (NUM($stock_silica) < 10) {
choice21 = LABEL('silica');
}
if (NUM($stock_sulphate) < 10) {
choice22 = LABEL('sulphate');
}
if (NUM($stock_sulphitest_sulphite) < 10) {
choice23 = LABEL('sulphitest');
}
if (NUM($stock_zinc) < 10) {
choice24 = LABEL('zinc');
}
if (choice1 == null && choice2 == null && choice3 == null && choice4 == null && choice5 == null && choice6 == null && choice7 == null && choice8 == null && choice9 == null && choice10 == null && choice11 == null && choice12 == null && choice13 == null && choice14 == null && choice15 == null && choice16 == null && choice17 == null && choice18 == null && choice19 == null && choice20 == null && choice21 == null && choice22 == null && choice23 == null && choice24 == null) {
SETRESULT('');
} else {
CONCAT (choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10 + choice11 + choice12 + choice13 + choice14 + choice15 + choice16 + choice17 + choice18 + choice19 + choice20 + choice21 + choice22 + choice23 + choice24);
}
user array
var choice = [];
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue';
}
if (NUM($stock_absorvent_pads) < 10) {
choice[1] = 'absorvent_pads';
}
if (choice.length == 0){
SETRESULT('');
} else {
concatStr = arr.join(" ");
}
var choice = []; /*Using array notation instead of using 24 separate variables*/
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue'; /*storing value in array rather than in variables*/
}
,..
,..
if (NUM($stock_zinc) < 10) {
choice[23] = LABEL('zinc');
}
/* Using for loop to iterate through array to check for null values. This is easier than having '==' null check for all 24 variables*/
for(var i=0;i<24;i++) {
if(choice[i] == null) {
SETRESULT('');
break;
}
}
/*If none of the values are null, then concat the array elements. Array.join is so easier than concatenating 24 string variables.*/
if(choice.length==24) {
concatStr = choice.join(" ");
}
Hope this explains.

Assigning static data amount in a div to be called from a function

I'm using an example based on the following example:
http://jsfiddle.net/5tt7d3e6/
In this, a function is created to turn a number into words.
The function is processed in the following HTML
<input type="text" name="number" placeholder="Number OR Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
<div id="word"></div>
The above allows you to enter a number into a textbox. The function translates what you type as a number into words.
Is there an easy way to set a div which already holds the number, instead of typing it to display?'
Such as:
<div data="innerHTML=convertNumberToWords(1233213)"></div>
You can hookup an event handler for onreadystatechange and inside that you can put your logic.
Fiddle 1
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(1233213);
};
However if you want show data based on existing value inside input, first put an id attribute for input
<input id="number" ... />
And then in JS:
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(number.value);
};
Fiddle 2
Suggestion: use switch statment in convertNumberToWords()
switch(amount){
case 0: 'zero'; break;
default: 'Please enter number only!'; break;
}
If you want to write to the document as it is processed and not later.
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
This is the recommended way though:
<div id="word"></div>
<script>
document.getElementById('word').innerHTML = convertNumberToWords(12233456);
</script>
--
<script>
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
</script>
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
Fiddle Link
change the value of textbox and press click button it will return into word
<input id="check"type="text" name="number" placeholder="Number OR Amount" />
<div id="word"></div>
<input type="button" id="button" value="click">
js code
$("#button").click(function()
{
var number = $('#check').val();
$("#word").html(toWords(number));
});
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}

HTML - show event of today with a JavaScript function

I want my HTML page to show me the events/ in this case birthday(s) of this day.
But my Code doesn't work.
function birthday() {
var event = [];
var temp = [];
event[0] = ["31.10.1991", "test1"];
event[1] = ["11.12.2015", "TestToday"];
var datum = new Date();
var today = today.getDate();
var month = today.getMonth() + 1;
for (i = 0; i < event.length; i++) {
if (event[i]) {
if (event[i][0] == today && event[i][0] == month) {
event[i][0] = temp[i];
}
}
else {
break;
}
}
if (temp.length == 0) {
document.write("Today nobody has a birthday");
}
else {
var x2 = "Today " + ((temp.length == 1) ? "has" : "have");
for (i = 0; i < temp.length; i++) {
x2 += ((temp[i] > 0) ? ((temp[i] == (temp.length - 1)) ? " and " : ", ") : " ") + temp[event[1]][3] + "(" + temp[event[i]][2] + ")";
}
document.write(x2 + " birthday");
}
}
Question1: Where are my mistakes and how can I make it work?
Question2: How can the program take the dates out of a excel sheet?
In JavaScript you define arrays using [];
I don't speak German so i don't know what you need to print
function birthday() {
var Geb = [];
var ausgabe = [];
Geb[0] = ["31.10.1991", "Test1"];
Geb[1] = ["23.11.2000", "Testheute"];
var datum = new Date(); //Datums Objekt
var heute = datum.getDate(); //Tage 1-31
var monat = datum.getMonth() + 1; //Monate von 0-11
for (i = 0; i < Geb.length; i++) {
if (Geb[i]) {
if (Geb[i][0] == heute && Geb[i][0] == monat) {
Geb[i][0] = ausgabe[i];
}
}
else {
break;
}
}
if (ausgabe.length == 0) {
document.write("Heute hat niemand Geburtstag");
}
else {
var x2 = "Heute " + ((ausgabe.length == 1) ? "hat" : "haben");
for (i = 0; i < ausgabe.length; i++) {
x2 += ((ausgabe[i] > 0) ? ((ausgabe[i] == (ausgabe.length - 1)) ? " und " : ", ") : " ") + ausgabe[Geb[i1]][3] + "(" + ausgabe[Geb[i]][2] + ")";
}
document.write(x2 + " Geburtstag");
}
}
birthday();

How to use AJAX or JSON in this code?

I am creating a website application that allows users to select a seat, if it is not already reserved, and reserve it.
I have created a very round about way of getting the seats that are previously reserved using iFrames, however that was temporarily, now I need to make it secure and "proper javascript code" using proper practices. I have no clue what AJAX (or JSON) is, nor how to add it to this code, but it needs to get the file "seatsReserved"+this.id(that is the date)+"Que.html" and compare the string of previously reserved seats to see which class to make the element. If this is horrible, or if any of the other things could work better, I am open to criticism to everything. Thank you all!
Here is the javascript code:
A little side note, all of the if statements are due to different amount of seats in each row
<script>
var i = " 0 ";
var counter = 0;
var leng=0;
document.getElementById("Show1").addEventListener("click", changeDay);
document.getElementById("Show2").addEventListener("click", changeDay);
document.getElementById("Show3").addEventListener("click", changeDay);
function changeDay() {
var iFrame = document.getElementById("seatList");
iFrame.src = "seatsReserved" + this.id + "Que.html";
document.getElementById('date').innerHTML = this.id;
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.removeEventListener("click", selectedSeat);
}
else {
document.getElementById(let +k).className = "openseat";
document.getElementById(let +k).removeEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
}
function loadChanges() {
var iFrame = document.getElementById("seatList");
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
var leng = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.addEventListener("click", selectedSeat);
seat.className = "openseat";
}
else {
document.getElementById(let +k).className = "notAvailible";
document.getElementById(let +k).addEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
i = " 0 ";
counter = 0;
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
document.getElementById("seatnums").innerHTML = counter;
}
i = document.getElementById("seatString").innerHTML;
counter = document.getElementById("seatnums").innerHTML;
function selectedSeat() {
var w = this.id;
var l = (" " + w);
var b = (" " + w + " ");
if (counter < 5) {
if (i.indexOf(b) <= 0) {
this.className = "closedseat";
i = i + b;
i = i.replace(" 0 ", " ");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter + 1;
document.getElementById("seatnums").innerHTML = counter;
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
function doNothing() {
}
var rannum = Math.random() * 1000;
document.getElementById('getConfirmation').value = rannum;
</script>

Categories

Resources