-2

I need a validation that contain enter space.

var addname = /^[0-9a-zA-Z\.,'";:& ]+$/;  
if(!(adres.value.match(addname))) {
   document.getElementById("ad").innerHTML="Feild Ruquired! only characters A-Z,0-9, a-z,and ',.;&:' are  acceptable";
   chk="false";
}

I use this code and it's working but when I press the enter button and write something in textarea field I get this error:

Feild Ruquired! only characters A-Z,0-9, a-z,and ',.;&:' are  acceptable

so I enter and write more description in a textarea field, like

1-somethong
2-something etc.
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Arslan Khalil
  • 41
  • 2
  • 2
  • 6

1 Answers1

0

You have to add an allowance for new lines in your regular expression A carriage return uses the invisible characters \r and a line break is \r\n.

So we just change your expression to:

var addname = /^[0-9a-zA-Z\.,'";:&\r\n ]+$/i; 

And it should catch them fine. ( i added the i at the end to make it case insensitive as well.)

you can test and play with it Here.

Hop this helps.

TinMonkey
  • 1,832
  • 1
  • 10
  • 7
  • Thnx for your reply.. but how alllow a enter space in regular expression – Arslan Khalil Oct 21 '15 at 09:47
  • If you can post some sample text to compare against, I can show you how to adjust the expression to allow it. – TinMonkey Oct 22 '15 at 15:59
  • var compdiscrip = /^[A-Za-z\.,-_"';&: ]+$/; this exprssion is worked but i press enter button ans write some discripion in textarea the experssion is run and feild is requied is show ... i want i write something after press a enter – Arslan Khalil Oct 22 '15 at 16:01
  • Oh... so the form is trying to submit when you press enter instead of adding a return into the text field? Here is a tread about that issue. http://stackoverflow.com/questions/9671688/preventing-enter-from-submitting-form-but-allow-it-on-textarea-fields-jquery – TinMonkey Oct 22 '15 at 16:07
  • Thnx,, but i think u not understand i want to say..... i say i have cantact form and there us textarea name of message.... when i write in it simple it wirte and worked fine... but i hit enter key and write something again in textarea filed and press submit it not submit , and textarea feild show error message feild is required... – Arslan Khalil Oct 22 '15 at 16:20
  • i want when i hit enter key and write something ,,,and submit it easily submit what thing i used in regular expression ,,that is work for that – Arslan Khalil Oct 22 '15 at 16:22
  • I updated the answer. Should solve your problem now. – TinMonkey Oct 22 '15 at 17:49