Jump to content

[SOLVED] How to pass nothing to a function


garethhall

Recommended Posts

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);


}

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

// 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);
}

Link to comment
Share on other sites

'', 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.