0

Hi I am novice in regex please help me on it: i've tried below but its failing (browser is ie9)

<html>
<head>


<script language="JavaScript">
function myf(){
alert(document.forms["Home"]["ww"].value);
var f=document.forms["Home"]["ww"].value;
var patt = new RegExp("[A-Z]a-z0-9().,-]");
if(patt.test(document.forms["Home"]["ww"].value))
alert(10);
}
</script>
</head>
<body >
<form method="" action=""  name="Home">
<input type="text" name="ww" value="123" onblur="myf()" >
</form>
</body>
</html>
Adward
  • 31
  • 5
  • The "]" after the "Z" is going to mess it up. You can test in console. The best way to start with regular expressions when you're learning is to build them up one item at a time - make it match letters, then letters and numbers, etc., testing at each point along the way. – Michael Chaney Oct 14 '14 at 12:33
  • What do you want to allow? Add some letters other than the code in your question body. – Avinash Raj Oct 14 '14 at 12:33
  • "which except only AlphaNumeric _-(). and space in " its there in the question body..thanks for your help – Adward Oct 14 '14 at 12:34
  • What do you mean by `except`? not to allow? – Avinash Raj Oct 14 '14 at 12:36
  • @AvinashRaj this is second time I'm seeing someone reply to you like that ;) – Amit Joki Oct 14 '14 at 12:36

2 Answers2

1

Try this as your expression:

/^[A-Za-z0-9().,-]+$/g

or

var patt = new RegExp("^[A-Za-z0-9().,-]+$");

You want to check if entire string from the beginning to the end contains only allowed characters.

strah
  • 6,702
  • 4
  • 33
  • 45
0

Change your patt to if you want to match only them, or add a ^ before \w which will negate the class.

var patt = /^[\w(),.\s-]+$/g
Amit Joki
  • 58,320
  • 7
  • 77
  • 95