Hi I use the following form validation and it works all fine, except my email input does not get validated. Input type = email field name = emailto this is the code: function formCheck(formobj){ // Enter name of mandatory fields var fieldRequired = Array("emailto","from", "emailfrom","zigi"); // Enter field description to appear in the dialog box var fieldDescription = Array("Friends E-mail","Friends Name", "Your E-mail","Your Name"); // dialog message var alertMsg = "These fields cannot be left blank:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired[i]]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "email": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; } var strValue = emailto.obj.value; var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i; if (!strFilter.test(strValue)) alertMsg = "wrong or incomplete email" + fieldDescription[i] + "\n"; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg += " - " + fieldDescription[i] + "\n"; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } } PHP: What am I missing in the email validation, please? Thank a lot
when you want to create a new Array, you need to do: var a = new Array(..); // or var a = ['email', 'from'] Code (markup): now, form.elements is an array, not as object, so you can't reference form.elements['emailto']. instead you'll need to do a for loop on the form.elements and compare the elements: for (var i = 0; i < form.elements.length; ++i) { var cur_element = form.elements[i]; switch (cur_element.nodeName) { case 'select': if ([B]cur_element.getAttibute('multiple') != undefined[/B]) { // its multiple } else { // its single } break; case 'textarea': [B]var value = cur_element.innerHTML[/B] break; case 'input': if ([B]cur_element.getAttribute('type') == 'text'[/B]) { // its a value type=text } } } Code (markup): i also changed the way you look up values of different elements. i'm not sure this works entirely, but i hope i gave you a good start
Hi Grunt... / Starcaller I have little experience in javascript, got this code a while ago from a guy who is not contactable. Array/Nodelist? what is my code then? the code, as it stands works OK as it validates the form as in "not allowing the fields to be kept empty" so one need to fill the fields. Hence, array or nodelist, even though I am ignorant of their distinct differences in coding requirements to successfully add an EMAIL VALIDATION. as the code stands now, it seems to work with either. Again, All I needed is to blend in the email validation into the existing code without having to change the entire code. My guess of that being possible, is my guess impossible to turn into reality? Cheers
Just curious ... Why would I need to change the entire code structure now? As I said in my own quote, the code works just fine as it is, I just needed the EMAIL VALIDATION to be corrected this ... ... wondering where the argument about "array" or "nodelist" is coming into or the argument about "object" or "element". If that would all be an argument... WHY is the form validation then working with the code I have at the moment? Shouldn't it be like ... the entire code does not works at all? Fact is, regardless of; array, nodelist, object and element ... It works either way. Just the email validation. So if someone could explain to me why we talk about something else instead of my question, it would help me understand why there is no need to solve my question but create some problem that does not exist. Cheers
That's my fault, I was curious what a bunch of form elements were. The differences are that nodeLists *look* like real arrays but you can't do stuff with them like .push and .pop (like with *real* arrays). When something looks like a duck and quacks like a duck, I'd like to be able to assume it's a duck. This line looks funny, but as a n00b myself, that's not saying much: var strValue = emailto.obj.value; why isn't is emailto.value? Though while I see there's some Array with a list of Thingies I don't see where those are actually being linked to some actual existing form. I would argue against this one that was suggested: case 'textarea': var value = cur_element.innerHTML because a textarea is a form control and it has a .value just like any other. I've used textarea.value (after defining what a textarea was) myself. So what's going on? You have your own form, and you took a script with does something like what you want for all the other form fields, except a new one you've added to your form, the email? Assuming this: <label for="email">Your email address:</label> <input type="text" name="email" id="email" value="" > you have this: case "email": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription + "\n"; } //all that does is check to see if it's been filled with anything at all... this part works? var strValue = emailto.obj.value; //Is this correct??? why isn't it email.value? obj is your general I-dunno-which-element placeholder but inside this case we know it's "email" var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i; if (!strFilter.test(strValue)) alertMsg = "wrong or incomplete email" + fieldDescription + "\n"; } break; // is this part working? default: } Is it skipping right to the default? How do you know your case matched correctly? The regex doesn't look bad so long as you don't have anyone using non-western letters or any of the other allowed-but-uncommon characters (I guess it's legal to have an email like #?*!@foo.name). I myself suck at figuring out how to use debugging software but I've seen someone with firebug be able to add breakpoints where your code pauses for a moment while you check values. You should check that -the case is matching the "email" obj -that emailto.obj.value is correct cause I keep thinking it should be emailto.value
Hi Stomme Thanks for your reply and I will take this into my php lesson (good explanation you gave here) Though, I have changed a bit in the meantime, using "colorbox" and it's "iframe" class to display the form. Now I face a little flash embedding hick-up ... posted it here ... http://forums.digitalpoint.com/showthread.php?t=1658834#post13398794 Thank a lot again.