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

regex expression problem

Status
Not open for further replies.

akaballa123

Technical User
Apr 29, 2008
46
US
Hi,

I am a newbie to reg expressions but trying to create one to control what is typed in a form field. If certain characters are found, an alert box pops up a msg when the user clicks submit. I tried my code, but it is not working..im sure there is something wrong with the regex...plz check it out:

Code:
function nameRegex(str)
   {
      return /([a-zA-Z0-9])+!?><([a-zA-Z0-9_-])+/.test(str);
   }
   function checkName(str)
   {
      if(nameRegex(str) == false)
      {
         return false;
      }
   }


 //Check the form for invalid or blank contents
  function checkForm()
  {
 var myForm = window.document.trainingForm;
   var ctName = myForm.tName;
if((ctName.value == '') || (checkName(ctName.value) == false))
     {
       alert('Please enter an appropriate course name');
       ctName.focus();
       return false;
     } 
}

thanks!
 
Oh im sorry..i thought I responded to that problem...well I found out that there is a date parse function that you can use. So I parsed the dates and then compared them in one line..

Code:
if(Date.parse(cStartD.value) > Date.parse(cEndD.value))
    {
       alert('ERROR: Start Date is greater than the end date. Please re-check your dates!');
       return false;
       cStartD.focus();
    }

Thanks for the help!
 
I'm certainly no expert in regular expressions (especially not in JS) but it looks like /([a-zA-Z0-9])+!?><([a-zA-Z0-9_-])+/ is looking for the following:
Any letter or number, 1 or more times,
followed by "!", one time or not at all,
followed by "><",
followed by a letter or number or "_" or "-", 1 or more times.

Is that what you intended?

_________________
Bob Rashkin
 
oh wow...im way off...i was intending for some thing where..within the string any character that is [highlight] ?!><[/highlight]. haha, i need to study this regex expressions. I was referring to some expression on one of the websites.
 
Like I said, I'm no expert. I could have gotten it wrong. On the off chance that I'm right, since "?" has a special meaning (one or no times), you would need to "escape" it: \?.

_________________
Bob Rashkin
 
im still not getting it to work. ANy ideas on how to implement this expression?
 
the expression doesnt seem to be working, unless i did not implement it correectly:

this is what I did:

function nameRegex(str)
{
return /[\?!><]/.test(str);
}
function checkName(str)
{
if(nameRegex(str) == true)
{
return false;
}
}

if((ctName.value == '') || (checkName(ctName.value) == false))
{
alert('Please enter an appropriate course name');
ctName.focus();
return false;
}

This validation does not occur when I enter one of the characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top