DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   HTML and CSS (http://www.daniweb.com/forums/forum143.html)
-   -   Validate textboxes (http://www.daniweb.com/forums/thread32044.html)

aish Sep 10th, 2005 12:28 am
Validate textboxes
 
useing this code I avoid to paste invalid chracters to textboxes.

function PasteAlphaNumeric(objEvent)
{
var strPasteData = window.clipboardData.getData("Text");
var objInput = objEvent.srcElement;
if (strPasteData.search("[^A-Za-z0-9]")!=-1) {
alert("The character you attempted to paste is not allowed for this field.");
objInput.focus();
return false;
}
}

I want to allow to paste white space characters (\s) and[A-Za-z0-9] to the textbox, how can I do this.

tgreer Sep 10th, 2005 12:55 am
Re: Validate textboxes
 
You could use an expression modifier, to add the /s to your character array. I haven't tested it, and it's late, but try:

function PasteAlphaNumeric(objEvent)
{
  var strPasteData = window.clipboardData.getData("Text");
  var objInput = objEvent.srcElement;
  if (strPasteData.search("[^A-Za-z0-9]+/s")!=-1)
  {
    alert("The character you attempted to paste is not allowed for this field.");
    objInput.focus();
    return false;
  }
}

aish Sep 10th, 2005 1:53 am
Re: Validate textboxes
 
thanks for your advice, but it was not working, any other ideas?

tgreer Sep 12th, 2005 2:51 pm
Re: Validate textboxes
 
Sorry. Include the "\s" special character within your character class. You'll need to escape the blackslash WITH a backslash. For example this works:

function testRegEx(x)
{

  var regex = new RegExp("[^A-Za-z0-9\\s]");

  if (x.value.search(regex) != -1)
  {
    alert("Invalid Entry.");
  }
}

aish Sep 14th, 2005 3:13 am
Re: Validate textboxes
 
Thanks!


All times are GMT -4. The time now is 2:31 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC