JavaScript - Regular Expression - Asked By theiry henry on 27-Jan-10 03:54 AM

i want a regular expression for accepting only character and numbers and / and - but it should not accept space
plz can ny one give me d expression.?

plz reply me urgently.plzzz - theiry henry replied to theiry henry on 27-Jan-10 04:13 AM

plz reply me urgently.plzzz

re - Santhosh N replied to theiry henry on 27-Jan-10 04:21 AM

You can check here for the regular expression for various patters on strings and use them.

http://regexlib.com/DisplayPatterns.aspx?cattabindex=3&categoryId=4

Re - Huggy Bear replied to theiry henry on 27-Jan-10 04:42 AM

If you are using Regular expression validator below is the one

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="^[0-9a-zA-Z/]+$" ControlToValidate="TextBox1" runat="server" 
ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>

If you are using javascript below is the sample function containing the expression

 <script type="text/javascript">
        function ValidateString() {
            var value = document.getElementById("TextBox1").value;
            var myregexp = new RegExp("[0-9a-zA-Z/]$");
            if(myregexp.exec(value) == null)
            {
                alert("Invalid");
            }
            else
            {
                alert("valid");
            }
            
        }
 </script>
mv ark replied to theiry henry on 29-Jan-10 06:20 AM
Use this expression - ^[a-zA-Z0-9\-/]+$

The previous post does not handle the - character. As it is a metacharacter it has to be escaped \-

function IsMatch(str)
{
 mystring = str;
 //only character and numbers and / and - but it should not accept space
 if (mystring.match(/^[a-zA-Z0-9\-/]+$/))
 { alert("match");
 }
 else
 { alert("not a match");
 }
}