Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation... 1

Status
Not open for further replies.

nelco

Programmer
Apr 4, 2006
93
US
I have the following function in my application...

function validateNum(val, numb)
{
if (ValidNumber(val, true) == true)
{
var txt = val.value;
if (txt.length == numb) return true
else {alert('must be '+numb+ ' digits');return false;}
}
return false;

Then I have in my application :

<input name="TxtVendorNo" value="<%=strVndNo%>" size="9" maxlength="9" onBlur="return validateNum(this,6);"></td>

This was working perfectly. Now user says that he should be allowed to enter 6 digits or 9 digits. My validation is for 6 digits. How can I validate now for 6 or 9 digits.
Any help will be appreciated.
Thanks
 
>onBlur="return validateNum(this,6);"
[tt]onBlur="return validateNum(this,6)||validateNum(this,9)"[/tt]
 
Easy enough.
Add an additional parameter to your function call
validateNum(this,6,9)

In your function
Code:
function validateNum(val, numb, numb2)
    {
         if (ValidNumber(val, true) == true)
         {
         var txt = val.value;
           if (txt.length == numb [COLOR=green]|| txt.length == numb2[/color]) return true
           else {alert('must be '+numb+ [COLOR=green]'or '+numb2+[/color]' digits');return false;}
         }
         return false;

This is not perfect as is, you will have to make certain that both numb and numb2 contain values before doing the compare or you could end up with an error if you were only testing one length instead of both. Easy enough for you to get started on though.


At my age I still learn something new every day, but I forget two others.
 
Or tsuji's way will work. :)

Personally, I would break this up into multiple function calls. One function to test for a valid number, one function to test the length and then call both functions from a main validation function.

By keeping each test to a basic level you can reuse that test on any other fields. For instance if you end up with an alpha only field of a fixed length you just add a test for alpha characters but use the same function for testing length. You keep your code shorter and build a library of functions you will use over and over in other apps as well.

I use this function to test for whole positive numbers:
Code:
function whole_positive_number(value) {
  var re = /^\d+$/;
  return re.test(value);
}

I use this function to test if a string is a specific length or within a range:
Code:
function set_length(value, min, max) {
  //Test number of characters in value against min and max values.  If no value set for min it defaults 0 if no value for max it defaults to the length of value.
  var vmin=(min == '')?0:min;
  var vmax=(max == '')?value.length:max;
  var re=((value.length < vmin) || (value.length > vmax))?false:true;
  return re;
}

I basically build function for each unique type of test so they remain flexible and then call only the functions required for that particular field.


At my age I still learn something new every day, but I forget two others.
 
Thanks all. theniteowl's solution worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top