Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a function with an argument

    I wrote a script that checks if a form field has numbers using the isNaN() function. This part works, but since I have to do this for many fields, I't thouht I'd send the false results to a function. This part dosn't work. Maybe I am not writting the code properly. If anyone can help me, it would be appreciated. The code is below:


    if (!isNaN(HardwareSoftwareForm.PC_Quantity.value)) {
    errorNumMessage("Extension");
    }

    function errorNumMessage(thisVar) {
    alert ("You Have Entered an Invalid Value for "+thisVar+". Please Enter Only Numbers");
    HardwareSoftwareForm.+thisVar+.focus()
    return false;
    }


  • #2
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    try this
    Code:
    if (!isNaN(HardwareSoftwareForm.PC_Quantity.value)) {
    return errorNumMessage("Extension"); 
    }
    or you could use regular expressions...
    Code:
    function checkNumeric(objElement)
    {	var re = /^[0-9]*$/;
    	if (!re.test(objElement.value))
    	{	alert("You Have Entered an Invalid Value for "+objElement+". Please Enter Only Numbers!");
    		objElement.value = objElement.value.replace(/[^0-9]/g,"");
    		objElement.focus();
    		objElement.select();
    		return false;
    	}
    }
    - NS 666
    .net DEVILoper

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem with your function is in this line:
    Code:
    HardwareSoftwareForm.+thisVar+.focus()
    Assuming that thisVar is the name of your element, you can try changing it to:
    Code:
    document. HardwareSoftwareForm.thisVar.focus();
    (I added "document" to the beginning, got rid of the +'s around thisVar, and put a semi-colon at the end). Note also that this requires that your fields have unique names in order for this to work properly (normally only id's need to be unique).

    Nischumacher's function would also work (I'd actually prefer this function...). In order to run this function correctly, you'd probably call it using the field's onchange event:
    Code:
    <input type="text" name="Quantity" id="numericField1" onchange="checkNumeric(this)">
    ...although you could also do it during the form's onsubmit; you'd need to cycle through each element in the form in order to do this. Note that Nischumacher's function passes an object (which is why I used "this" when calling it). A slight change to the function to make it work correctly:
    Code:
    alert("You Have Entered an Invalid Value for "+objElement.name+". Please Enter Only Numbers!");
    This assumes that the field's name had meaning to the user...

    One last thing... it's been noted that IE may have problems with the .focus() and .select() calls (esp. when the functions are being called from either onblur or onchange events). Two things you may need to do: 1) set "returnValue=false;" in addition to "return false;" and 2) use setTimeout("objElement.focus()", 100); to allow the event function to complete before trying to shift focus.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #4
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Pyth007
    A slight change to the function to make it work correctly:
    Code:
    alert("You Have Entered an Invalid Value for "+objElement.name+". Please Enter Only Numbers!");
    This assumes that the field's name had meaning to the user...
    you are right... thank you for correcting me... and for explaning as well...

    Quote Originally Posted by Pyth007
    One last thing... it's been noted that IE may have problems with the .focus() and .select() calls (esp. when the functions are being called from either onblur or onchange events). Two things you may need to do: 1) set "returnValue=false;" in addition to "return false;" and 2) use setTimeout("objElement.focus()", 100); to allow the event function to complete before trying to shift focus.
    interesting info... thanks again...
    - NS 666
    .net DEVILoper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •