PDA

View Full Version : Simple OR operator question



Ty44ler
Nov 19th, 2009, 07:16 PM
I'm trying to not allow my users to enter in n/a in my form ex... "N/A" "n/A" "na"

My code trips the alert every time no matter what is entered in the cell... Why is it doing that?


function appliance() {
if (document.ComplianceForm.Appliance.value=="n/a" || "N/A" || "n/A" || "N/a") {
alert("You cannot enter any variation of n/a in this cell.")
}
else {
return false;
};
}

Ty44ler
Nov 19th, 2009, 07:41 PM
Nevermind I found it, you have to compare the value of the cell or else it will always return true...


function appliance() {
var appliance_value = document.ComplianceForm.Appliance.value;
if (appliance_value=="n/a" || appliance_value == "N/A" || appliance_value == "n/A" || appliance_value == "N/a") {
alert("You cannot enter any variation of n/a in this cell.")
}
else {
return false;
}
}

Philip M
Nov 19th, 2009, 07:45 PM
Your conditions have incorrect syntax. Should be:-

var CFA = document.ComplianceForm.Appliance.value;
if ((CFA == "n/a") || (CFA== "N/A") || (CFA == "n/A") || (CFA== "N/a")) {

But a simpler/better test (which traps n/a regardless of case anywhere in the string - if the user enters space n/a that is not trapped in your code) is:-



function appliance() {
var CFA = document.ComplianceForm.Appliance.value;
if (/(n\/a)|(n\.a)/gi.test(CFA)) { // traps n.a as well
alert("You cannot enter any variation of n/a in this cell.");
document.ComplianceForm.Appliance.value = "";
document.ComplianceForm.Appliance.focus();
return false;
}
}


"Remember that there is nothing stable in human affairs; therefore avoid undue elation in prosperity, or undue depression in adversity". Socrates - Greek philosopher in Athens (469 BC - 399 BC)

Old Pedant
Nov 19th, 2009, 09:31 PM
Or even simpler:


function appliance()
{
var fld = document.ComplianceForm.Appliance;
if ( fld.value.toLowerCase().indexOf("n/a") < 0 ) return true;
alert("You cannot enter any variation of n/a in this cell.");
fld.value = "";
fld.focus();
return false;
}

Ty44ler
Nov 20th, 2009, 02:10 PM
If I wanted to check all my text boxes in the form would it be correct to use:


var fld = document.getElementById("check").value

then put an id="check" to all the fields I want to be checked?
I've tried it before, but it didn't seem to work correctly.

Philip M
Nov 20th, 2009, 03:30 PM
If I wanted to check all my text boxes in the form would it be correct to use:


var fld = document.getElementById("check").value

then put an id="check" to all the fields I want to be checked?
I've tried it before, but it didn't seem to work correctly.

All id's must be unique - you cannot have two elements with the same id.

This is what you want:-


<form name= "ApplianceForm">
<input type = "text" name = "T1" onblur = "appliance(this)"><br>
<input type = "text" name = "T2" onblur = "appliance(this)"><br>
<input type = "text" name = "T3" onblur = "appliance(this)"><br>
<input type = "text" name = "T4" onblur = "appliance(this)"><br>
</form>

<script type = "text/javascript">
function appliance(which) {
var val = which.value;
if (/(\s*n\s*\/\s*a)|(\s*n\s*\.\s*a)/gi.test(val)) { // traps n.a as well, and also traps inserted spaces
alert("You may not enter any variation of n/a in this cell.");
which.value = "";
which.focus();
return false;
}
}
</script>

Assuming that the user is required to make an entry in each textbox (other than n/a which is not allowed), then you need to check for empty textboxes by calling a validation function onsubmit. Depending on the expected sensible responses, you should check using if (field.value.length < 8) or whatever to avoid people entering just x or ?

Ty44ler
Nov 20th, 2009, 04:06 PM
Thank you, that works!

I have noticed however that firefox doesn't like it too much. If n/a is entered in one field and triggers a response it takes the focus to the next one and n/a does not trigger an alert if entered into it. However, this is not a big deal as the users I'm writing it for only use IE.

Philip M
Nov 20th, 2009, 05:43 PM
Thank you, that works!

I have noticed however that firefox doesn't like it too much. If n/a is entered in one field and triggers a response it takes the focus to the next one and n/a does not trigger an alert if entered into it. However, this is not a big deal as the users I'm writing it for only use IE.

Ah yes, this is a bug in Firefox which I ought to have remembered.

Change the script to as below and it will work in all browsers:-


<script type = "text/javascript">
function checkNoNA(which) {
var val = which.value;
if (/(\s*n\s*\/\s*a)|(\s*n\s*\.\s*a)/gi.test(val)) { // traps n.a as well, also inserted spaces
alert("You cannot enter any variation of n/a or n.a in this cell.");
which.value = "";
myfield = which; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10);
return false;
}
}
</script>