Solved

validate if a textbox in a datagrid allows any special symbols other than "|"(pipe)  to be entered using javascript

Posted on 2008-10-28
11
376 Views
Last Modified: 2012-05-05
Hi!

I have a datagrid.I have a textbox in one of the columns  in the datagrid.
The textbox should allow numbers and  a "|". it should not allow any special characters other than pipe.(|)
The validation ha sto be done using javascript.(not by using validation controls)
I need it ASAP.

IT'S EXTREMELY URGENT.

CAN I GET THE CODE/LOGIC FOR THAT asap.

thanks  a million in advance.
0
Comment
Question by:amukta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22828751
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript">
function validate(o){
	if( /[^\d|]/.test(o.value) )
	{
		alert("Invalid char found. Only digits and | are allowed.");
		setTimeout(function(){focusIt(o)},10);
	return false;
	}
return true;
}
function focusIt(o){
	o.focus();
}
</script>
</head>
<body>
<input type="text" onchange="validate(this)"/>
</body>
</html>

Open in new window

0
 

Author Comment

by:amukta
ID: 22828908
it is still allowing to insert "|"(pipe.)
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828914
I know. You said:
>> it should not allow any special characters other than pipe.(|)

So, what is it you want?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:amukta
ID: 22828915
what is test(o.value)?
0
 

Author Comment

by:amukta
ID: 22828921
I need it ASAP.
0
 

Author Comment

by:amukta
ID: 22828922
i NEED IT asap.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828950
>>what is test(o.value)?
o.value is the value of the text box

>>I need it ASAP
And if you don't clarify what you want, you are just delaying the process. I gave you EXACTLY what you asked for. So, if you mistyped the question you need to clarify.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828953
If you want ONLY digits then use:
if( /[^\d]/.test(o.value) )
0
 

Author Comment

by:amukta
ID: 22836136
Is test(o.value) a function?
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22836272
>>Is test(o.value) a function?
Not a function. It is a method. Given this:
if( /[^\d|]/.test(o.value) )

get rid of the "if" clause and you are left with:
/[^\d|]/.test(o.value)

So, you can clearly see that I am using a RegExp object. The if statement I wrote is equivalent to:

var t = new RegExp("[^\\d|]", "");
if( t.test( o.value) )

if you are NOT familiar with reular expressions I suggest you refer to:
http://www.webreference.com/js/column5/index.html

If you are already familiar with regular expressions, then skip to:
http://www.webreference.com/js/column5/methods.html

so you become familiar with the supported methods.

On another note, still not clear what the issue is. You said you wanted to allow pipes, which it does. But then you said you don't, in which case just remove the pipe symbol from my original regex and it will allow only digits
0
 

Author Comment

by:amukta
ID: 24716716
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question