Click to See Complete Forum and Search --> : Javascript.


DanUK
03-13-2003, 10:40 AM
Hi, if the below is the code (which works - there is more but the below is just an example) to verify a form, with just A to Z, a to z and 0 to 9 text can you please tell me what I'd need to put to make a 'checkbox' required please?

else if (!/^[a-z0-9]+$/i.test(form.contactNAME.value)) {
alert("Please include your name for contact purposes.");
form.contactNAME.select(); }

Any help you can give me will be much appreciated, thanks!

AdamBrill
03-13-2003, 11:02 AM
You can check if a checkbox is checked like this:

if(form.CheckBoxName.checked==true)

So, if you want to add that into yours, it would be like this:

else if (!/^[a-z0-9]+$/i.test(form.contactNAME.value) && form.CheckBoxName.checked==true)

I hope that helps... ;)

boojum
03-13-2003, 11:38 AM
"with just A to Z, a to z and 0 to 9"

isnt [a-z0-9]+ missing the capitol alphabet?

AdamBrill
03-13-2003, 12:16 PM
boojum - In RegEx, if you put an i after it, it will make it so it isn't case sensative. /^[a-z0-9]+$/ would only work with small letters and numbers, while /^[a-z0-9]+$/i works with capital letters, too. ;)

DanUK
03-13-2003, 04:54 PM
so like this?

if(form.CheckBoxName.checked==true) {
alert("Please make sure the checkbox is ticked!");
form.CheckBoxName.select(); }


this is ok?
thanks!

Jona
03-13-2003, 04:58 PM
You don't even have to have the ==true on there. if(form.CheckBoxName.checked) should do just fine.

P.S. Right, guys??? ;)

DanUK
03-13-2003, 04:59 PM
if(form.CheckBoxName.checked) {
alert("Please make sure the checkbox is ticked!");
form.CheckBoxName.select(); }

^^^ it's ok now ? :) :confused:

Jona
03-13-2003, 05:03 PM
Yah, I just made this:

<script>
function blah(){
if(document.forms[0].CheckName.checked){ document.forms[0].CheckName.select(); alert('ha!!'); }
}
</script>
<form><input type=checkbox name=CheckName> <input type=button onclick="blah()" value="hahahah"></form>

It works, but it's kinda controversial. You see, you want if it's NOT checked, so you could do if(!document.forms[0].CheckName.checked) instead.

DanUK
03-13-2003, 05:05 PM
:D Thanks! :D