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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple reg expression

Status
Not open for further replies.

teroy

Programmer
Oct 17, 2000
67
AU
Hi there,
I was wondering if anyone knew the syntax to write code for the following.

I want to have an alert box that pops up if the value in a input type contains a non alpha numeric character ie "%$#"

so i would do the following

function checkform()
{
if ("don't know what syntax to use in here"){
alert("You must enter a value which only contains alphanumeric characters")
return false
}
else{
return true
}
}

<FORM name=&quot;form&quot; method=&quot;post&quot; action=&quot;&quot; onSubmit=&quot;return checkform()&quot;>
<input type=text name=bleah>
<snip>
</form>


Thanks in Advance
 
Here is an excellent JS library for form validation I found a couple of years ago at It should be pretty easy to modify these to fit your needs:

Code:
// Validate.js
// Some Routines for HTML-Form Validation


  function returnSelection(radioField)
    {
    var selection = null;
    for (i=0; i < radioField.length; i++)
        {
        if (radioField[i].checked)
           {
           selection=radioField[i].value;
           return selection; 
           }
        }
    return selection; 
    }

  // Check if Field contains something
  function ContainsSomething(Field)
   {
   if ((Field.type == &quot;text&quot;) || (Field.type == &quot;textarea&quot;))
      {
      if (Field.value == &quot;&quot;)
         {
         return false;
         }
      }
   else
      {   
      if (returnSelection(Field) == null)
         {
         return false;
         }
      }

   return true;
   }
   
  // Check for valid (ie containg &quot;@&quot;, &quot;.&quot;, 
  // and more than 6 characters) email-address in Field
  function IsValidEmail(Field)
    {
   if (!ContainsSomething(Field))
      {
      return false;
      }
    if (Field.value.indexOf(&quot;@&quot;)==-1  
        || Field.value.indexOf(&quot;.&quot;)==-1 
        || Field.value.indexOf(&quot; &quot;)!=-1 
        || Field.value.length<6)
       {
       return false;
       }
    else
       {
       return true;
       }
    }   
  
  // Check if Field contains a valid date of the form dd/mm/yy
  function IsValidDate(Field)
    {
   if (!ContainsSomething(Field))
      {
      return false;
      }
    var indate=Field.value;
    var sdate = indate.split(&quot;/&quot;)
  
    var chkDate = new Date(Date.parse(indate))

    var cmpDate = (chkDate.getMonth()+1)+
                   &quot;/&quot;+(chkDate.getDate())+
                   &quot;/&quot;+(chkDate.getYear())
    var indate2 = (Math.abs(sdate[0]))+&quot;/&quot;+(
                   Math.abs(sdate[1]))+
                   &quot;/&quot;+(Math.abs(sdate[2]))
    if (indate2 != cmpDate || cmpDate == &quot;NaN/NaN/NaN&quot;)
       {
       return false
       }
    else 
       {
       return true;
       }	
    }

  // Check if Field contains numeric data only
  function IsNum(Field) 
    {
   if (!ContainsSomething(Field))
      {
      return false;
      }
    theNum = parseFloat(Field.value);
    if (Field.value != '' + theNum)
       {
       return false;
       }
    return true;
    }

  // Check if Field contains only letters
  function IsOnlyLetters(Field)
   {
   if (!ContainsSomething(Field))
      {
      return false;
      }
   var Letters = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZÜÖÄ'´&quot;
   for (i=0; i < Field.value.length; i++)
       {
       var CheckChar = Field.value.charAt(i);
       CheckChar = CheckChar.toUpperCase();
       if (Letters.indexOf(CheckChar) == -1)
          {
          return false;
          }
       }
       return true;
    }

  // Check if Field contains only digits in range Min to Max
  function IsInRange(Field, Min, Max)
    {
    if (IsNum(Field) == false)
       {
       return false;
       }
    if (Field.value < Min || Max < Field.value) 
       {
       return false;
       }
    return true;
    }

  // Check if Field is not equal to strCompare
  function IsNotEqual(Field, strCompare)
    {
    if (Field.value== strCompare)
       {
       return false;
       }
    return true;
    }

  function isCreditCard(st)
   {
   if (st.length > 19)
      {
      return (false);
      }
   sum = 0; mul = 1; l = st.length;
   for (i = 0; i < l; i++) 
       {
       digit = st.substring(l-i-1,l-i);
       tproduct = parseInt(digit ,10)*mul;
       if (tproduct >= 10)
          {
          sum += (tproduct % 10) + 1;
          }
       else
          {
          sum += tproduct;
          }
       if (mul == 1)
          {
          mul++;
          }
       else
          {
          mul--;
          }
       }
    if ((sum % 10) == 0)
       {
       return (true);
       }
    else
       {
       return (false);
       }
    }

  function IsVisa(cc)
    {
    if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
       {
       return isCreditCard(cc);
       }
    return false;
    } 
  function IsMasterCard(cc)
    {
    firstdig = cc.substring(0,1);
    seconddig = cc.substring(1,2);
    if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
       {
       return isCreditCard(cc);
       }
    return false;
    } 

  function IsAmericanExpress(cc)
    {
    firstdig = cc.substring(0,1);
    seconddig = cc.substring(1,2);
    if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
       {
       return isCreditCard(cc);
       }
    return false;
    } 

  function IsValidCC(Field)
    {
    tempString = &quot;&quot;;
    bag = &quot;- &quot;;
    for (i = 0; i < Field.value.length; i++)
        {   
        var c = Field.value.charAt(i);
        if (bag.indexOf(c) == -1) tempString += c;
        }
    cc = tempString;
    if (!isCreditCard(cc))
       {
       return false;
       }
  if (!IsMasterCard(cc) && !IsVisa(cc) && !IsAmericanExpress(cc))
     {
     return false;
     }
  return true;
  }

There is a lot more sophisticated code than this available if you really spend some time searching sites like etc...
 
Thanks for the functions rycamor! I guess what i need is make a combination of
function IsNum(Field)
and
function IsOnlyLetters(Field)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top