garethhall Posted May 28, 2009 Share Posted May 28, 2009 Hello Guys, Can some please me and tel how can I pass a null or no value to a function. I have the 2 scripts below and I am writing my JS this way so I can recall it and use it on multiple pages. But as I am sure you can see I don't have a form element called lastName and Fax thus my JS return an undefined error. So When I call me function how do I tell it that that element does not exists? I always thought I can just leave in blank or pass it nothing like this " " but I am wrong? HTML Page <form name="frmContact" method="post"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td align="right" width="15%" class="label">Name:</td> <td width="85%" ><input type="text" name="name" class="textfield"/></td> <td > </td> <td><span id="spn_name"></span></td> </tr> <tr> <td align="right" class="label">Phone:</td> <td ><input type="text" name="phone" class="textfield"/></td> <td > </td> <td><span id="spn_phone"></span></td> </tr> <tr> <td align="right" class="label">Email:</td> <td ><input type="text" name="email" class="textfield"/></td> <td > </td> <td><span id="spn_email"></span></td> </tr> <tr> <td align="right" valign="top" class="label">Message:</td> <td ><textarea name="message" cols="20" rows="5" class="textarea"></textarea></td> <td > </td> <td></td> </tr> <tr> <td align="right" valign="top" class="label"> </td> <td ><input type="button" name="submit" id="submit" value="Submit" onclick="validate('frmContact', 'name', '', 'email', 'phone', '', 'CMS/cmsImages/approvedMark.gif', 'CMS/cmsImages/rejectMark.png');"/></td> <td > </td> </tr> </table> </form> Javascript // Validation Filters var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var nameReg = /[A-Za-z\'\-]/; var phoneReg = /[0-9\(\)\-\+ ]{7,16}/; function validate(frmName, fieldName, fieldLastName, fieldEmail, fieldPhone, fieldFax, apprvMK, rejectMK){ // Set Variables to hold values and followed by Element Name var name = document.forms[frmName].elements[fieldName].value; var nameElementName = document.forms[frmName].elements[fieldName].name; var lastName = document.forms[frmName].elements[fieldLastName].value; var lastNameElementName = document.forms[frmName].elements[fieldLastName].name; var emailAddress = document.forms[frmName].elements[fieldEmail].value; var emailElementName = document.forms[frmName].elements[fieldEmail].name var phone = document.forms[frmName].elements[fieldPhone].value; var phoneElementName = document.forms[frmName].elements[fieldPhone].name; var fax = document.forms[frmName].elements[fieldFax].value; var faxElementName = document.forms[frmName].elements[fieldFax].name; // This runs the validation and updates the Page function runValidation(myField, MyReg, MyElementName){ if(!myField.match(MyReg)){ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+rejectMK+"' width='12' height='12' />"; }else{ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+apprvMK+"' width='12' height='12' />"; } } runValidation(name, nameReg, nameElementName); runValidation(lastName, nameReg, lastNameElementName); runValidation(emailAddress, emailReg, emailElementName); runValidation(phone, phoneReg, phoneElementName); runValidation(fax, phoneReg, faxElementName); } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Just remove it from the validation altogether. HTML <form name="frmContact" method="post"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td align="right" width="15%" class="label">Name:</td> <td width="85%" ><input type="text" name="name" class="textfield"/></td> <td > </td> <td><span id="spn_name"></span></td> </tr> <tr> <td align="right" class="label">Phone:</td> <td ><input type="text" name="phone" class="textfield"/></td> <td > </td> <td><span id="spn_phone"></span></td> </tr> <tr> <td align="right" class="label">Email:</td> <td ><input type="text" name="email" class="textfield"/></td> <td > </td> <td><span id="spn_email"></span></td> </tr> <tr> <td align="right" valign="top" class="label">Message:</td> <td ><textarea name="message" cols="20" rows="5" class="textarea"></textarea></td> <td > </td> <td></td> </tr> <tr> <td align="right" valign="top" class="label"> </td> <td ><input type="button" name="submit" id="submit" value="Submit" onclick="validate('frmContact', 'name', 'email', 'phone', 'CMS/cmsImages/approvedMark.gif', 'CMS/cmsImages/rejectMark.png');"/></td> <td > </td> </tr> </table> </form> JavaScript // This runs the validation and updates the Page function runValidation(myField, MyReg, MyElementName){ if(!myField.match(MyReg)){ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+rejectMK+"' width='12' height='12' />"; }else{ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+apprvMK+"' width='12' height='12' />"; } } // Validation Filters var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var nameReg = /[A-Za-z\'\-]/; var phoneReg = /[0-9\(\)\-\+ ]{7,16}/; function validate(frmName, fieldName, fieldEmail, fieldPhone, apprvMK, rejectMK){ // Set Variables to hold values and followed by Element Name var name = document.forms[frmName].elements[fieldName].value; var nameElementName = document.forms[frmName].elements[fieldName].name; var emailAddress = document.forms[frmName].elements[fieldEmail].value; var emailElementName = document.forms[frmName].elements[fieldEmail].name var phone = document.forms[frmName].elements[fieldPhone].value; var phoneElementName = document.forms[frmName].elements[fieldPhone].name; runValidation(name, nameReg, nameElementName); runValidation(emailAddress, emailReg, emailElementName); runValidation(phone, phoneReg, phoneElementName); } Quote Link to comment Share on other sites More sharing options...
garethhall Posted May 28, 2009 Author Share Posted May 28, 2009 I know but that is the easy way out I am wanting my code to be as reusable as it can be. If I take it out and say I have another page that does need to validate those 2 fields then I would have to make a separate function or script. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 // Validation Filters var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var nameReg = /[A-Za-z\'\-]/; var phoneReg = /[0-9\(\)\-\+ ]{7,16}/; function validate(frmName, fieldName, fieldLastName, fieldEmail, fieldPhone, fieldFax, apprvMK, rejectMK){ // Set Variables to hold values and followed by Element Name var name = document.forms[frmName].elements[fieldName].value; var nameElementName = document.forms[frmName].elements[fieldName].name; var lastName = document.forms[frmName].elements[fieldLastName].value; var lastNameElementName = document.forms[frmName].elements[fieldLastName].name; var emailAddress = document.forms[frmName].elements[fieldEmail].value; var emailElementName = document.forms[frmName].elements[fieldEmail].name var phone = document.forms[frmName].elements[fieldPhone].value; var phoneElementName = document.forms[frmName].elements[fieldPhone].name; var fax = document.forms[frmName].elements[fieldFax].value; var faxElementName = document.forms[frmName].elements[fieldFax].name; // This runs the validation and updates the Page function runValidation(myField, MyReg, MyElementName){ if(!myField.match(MyReg)){ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+rejectMK+"' width='12' height='12' />"; }else{ document.getElementById("spn_"+MyElementName).innerHTML = "<img src='"+apprvMK+"' width='12' height='12' />"; } } runValidation(name, nameReg, nameElementName); if (lastName !== '') runValidation(lastName, nameReg, lastNameElementName); runValidation(emailAddress, emailReg, emailElementName); runValidation(phone, phoneReg, phoneElementName); if (fax !== '') runValidation(fax, phoneReg, faxElementName); } Quote Link to comment Share on other sites More sharing options...
.josh Posted May 30, 2009 Share Posted May 30, 2009 <script language="javascript"> function blah(a,b,c) { if (a == null) alert('a is null'); } </script> <a href='' onclick="blah(1,2,3);">a is not null</a><br/> <a href='' onclick="blah(null,2,3);">a is null</a><br/> Quote Link to comment Share on other sites More sharing options...
bibby Posted June 1, 2009 Share Posted June 1, 2009 '', undefined, null, 0, NaN are all in the "nothing family" , in that they fail to pass if if(x){} They sometimes == each other, but not never === , such as undefined == null '' == false 0 == '' NaN is neither true, false, or null, but is still a number Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.