Forum Moderators: open
<script type="text/javascript">
<!--
function check6(obj){
//use the modulus operator '%' to see if there is a remainder
if(obj.value%6!=0){
alert('The number you entered is not divisible by 6')
}
}
//-->
</script>
The rest of the instructions in that post was to place:
onchange='check6(this)'
in the form to let the script know what to check. I placed it at the end of the <input=> statement.
I use an image for my submit button. If the image button is clicked, the user does get the message, but if it is pressed a second time, or if the user uses the "enter" key instead, the condition is ignored and any number can be entered in the field.
What am I doing wrong?
2. Ideally, you should put JavaScript in it's own file (not in your HTML file).
3. You should perform your validation in the form's onsubmit event instead of the onchange event of the input. To prevent the form from submitting, return false to the form's onsubmit handler.
function check6(obj) {
// use the modulus operator '%' to see if there is a remainder
if (obj.value % 6 != 0) {
alert('The number you entered is not divisible by 6');
return false;
}
return true;
}
// Attach your form's onsubmit handler
var f = document.getElementById('yourform');
f.onsubmit = function() {
var el = document.getElementById('yourinput');
return check6(el);
};
And in your form:
<form id='yourform' ...>
<input id='yourinput' ...>
Note, this script needs to be called after your form elements are defined. At the end of your document, just before the closing </body> tag is the best place to put your scripts (and like I said, linking to an external .js file is the best method).
Hope this helps.
Sorry if I seem confused, but I am aware that if I put this in the wrong place, it won't work.
Thank you.
<form action='...' method='...'>
<form id='SOMEUNIQUEVALUE' action='...' method='...'>
The same rules apply to the id attribute you give to input element. You shouldn't have to do anything with regards to the image/submit button.
Notice in my script the code that does this:
var f = document.getElementById('yourform');
var el = document.getElementById('yourinput');
Make sense?
function check48(n) {
return n >= 48;
}
function check6(n) {
// use the modulus operator '%' to see if there is a remainder
if (n % 6 != 0) {
alert('The number you entered is not divisible by 6');
return false;
}
return true;
}
// Attach your form's onsubmit handler
var f = document.getElementById('yourform');
f.onsubmit = function() {
var el = document.getElementById('yourinput');
return (check6(el.value) ¦¦ check48(el.value));
};
Note, I've changed the input of the check6 function to take a value instead of an object (no need to pass an element around, just the value).
Also note, this forum replaces the pipe character with ¦ so be sure to replace those characters with real pipe characters in your script.
Also...your message about validating on the server side. I use Hostmonster to host the web site on their servers and a 3rd party Cart service to process the orders. (If I can't completely understand design of a page yet...don't even want to try PCI compliance :o) Is there code that I need on my page for additional validation?
Now...about the server side validation. Do I need to do anything further?
BTW...you folks are awesome! I've left questions in other forums and either received no answer or some sarcastic response like I was a total idiot. The fact that my inquiries have not only been answered, but answered thoroughly with explanations, makes this the best forum for all my web design questions. Thanks to all!
The jscript was to validate the number put in the textbox and worked fine when I was testing only one item on the page. Now that I have populated the page with all colors available, none of the checkboxes will function and only the first text box will function.
If there is more than one text box on a page, does each text box need a unique id for jscript validation to work? Can the jscript var el contain more than one id?
Thank you.
f.onsubmit = function() {
var el = document.getElementById('yourinput');
if (!check6(el)) {
return false;
}
el = document.getElementById('anotherinput');
if (!check6(el)) {
return false;
}
// Repeat for each id ...
// ...
// if made it without returning false, then all are valid
return true;
};
[script]
function check48(n) {
return n >= 48;
}
function check6(n) {
// use the modulus operator '%' to see if there is a remainder
if (n % 6 != 0) {
alert('Order by half-dozen increments greater than 48');
return false;
}
return true;
}
// Attach your form's onsubmit handler
var f = document.getElementById('erb');
f.onsubmit = function() {
var el = document.getElementById('BLK');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('BKPIN');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('BWCHS');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('BLST');
if (!check6(el.value) && !check48(el.value)){
return false;
}
return true;
}
[/script]
What have I done incorrectly?
Here is the corrected code:
function check48(n) {
return n >= 48;
}
function check6(n) {
// use the modulus operator '%' to see if there is a remainder
if (n % 6 !== 0) {
alert('Order by half-dozen increments greater than 48');
return false;
}
return true;
}
// Attach your form's onsubmit handler
var f = document.getElementById('erb');
f.onsubmit = function() {
var el = document.getElementById('BLK');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('BUR');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('DEN');
if (!check6(el.value) && !check48(el.value)){
return false;
}
el = document.getElementById('HUN');
if (!check6(el.value) && !check48(el.value)){
return false;
}
return true;
};
Here I'll break down 4 examples, using values 6, 7, 60, 61:
if (!check6(6) && !check48(6))
if (!true && !false)
if (false && true)
if (false)
if (!check6(7) && !check48(7))
if (!false && !false)
if (true && true)
if (true)
if (!check6(60) && !check48(60))
if (!true && !true)
if (false && false)
if (false)
if (!check6(61) && !check48(61))
if (!false && !true)
if (true && false)
if (false)
As you can see, only the case that failed both of your validation functions (7) returned true.
You could fix this in one of two ways.
1. Move your logical NOT (!) operator:
if (!(check6(el.value) && check48(el.value))){
2. Change the && to an ¦¦
if (!check6(el.value) ¦¦ !check48(el.value)){
Here are the 4 examples again using these changes.
if (!(check6(6) && check48(6)))
if (!(true && false))
if (!(false))
if (true)
if (!(check6(7) && check48(7)))
if (!(false && true))
if (!(false))
if (true)
if (!(check6(60) && check48(60)))
if (!(true && true))
if (!(true))
if (false)
if (!(check6(61) && check48(61)))
if (!(false && true))
if (!(false))
if (true)
As you can see, all of the validation should work correctly. The && condition verifies that all of the individual tests passed, and then the ! converts the result (since this is checking if the validation did NOT pass).
Now the other approach:
if (!check6(6) ¦¦ !check48(6))
if (!true ¦¦ !false)
if (false ¦¦ true)
if (true)
if (!check6(7) ¦¦ !check48(7))
if (!false ¦¦ !false)
if (true ¦¦ true)
if (true)
if (!check6(60) ¦¦ !check48(60))
if (!true ¦¦ !true)
if (false ¦¦ false)
if (false)
if (!check6(61) ¦¦ !check48(61))
if (!false ¦¦ !true)
if (true ¦¦ false)
if (true)
This approach is really saying if any of the individual tests did NOT pass. Both of these methods will work for you, so choose whichever you will best be able to understand if you look at it later on.
1. Moved the logical NOT (!) operator:
if (!((check6(el.value) && check48(el.value))){
2. Changed the && to an ¦¦
if (!check6(el.value) ¦¦ !check48(el.value)){
Each does work, but only on the first input field (id='BLK'); also, none of the checkboxes on the page work when I use either of these.