2
<!DOCTYPE html>
<html>
<head><script type="text/javascript">
function onPaste(eve) {
    try {
        var txt = e.clipboardData.getData('text/plain');
        alert(txt);
    } catch (err) {
    }
} </script> </head><body>
<textarea cols=60 name="inputUsaaNum" onpaste="onpaste(event);"></textarea></body></html>

please help to validate the clipboard data from text area, i need to identify the whaether it has any spacial character and also i want to delimit the values with help of carriage return('\r'), please help????

jcrshankar
  • 1,165
  • 8
  • 25
  • 45
  • Then what would the data be? Where do you want to delimit the 'values'? Are you sure you want `\r`? Not `\n\r` or simply `\n`? What special characters would you want to identify, from what codepage? – GitaarLAB Aug 18 '12 at 05:45
  • Gitaar, i will copy the data from Excel sheet(card numbers) it should not have any special character . i want to delimit the value with help of space or '\r' only. – jcrshankar Aug 18 '12 at 05:51
  • i need only (1 to 9 and _ ) no other characters. – jcrshankar Aug 18 '12 at 05:52
  • So to further clarify, you don't need any help getting the data from the clipboard, the question is how to test whether the `txt` variable contains only 1-9 or underscore (not 0?) - and if the value is OK you want to do _something_ with space or `\r` but you have yet to explain what. If the input contains `12\r13\r14` would you like the values 12, 13 and 14 put in an array or something? Please update your question with an example input and output. – nnnnnn Aug 18 '12 at 06:06

1 Answers1

1

According to your last comments you want something like this:
txt.replace(/[^1-9_\t ]/g,'').replace(/[\t]/g,'\r');

This will first replace everything that is not 1 to 9 or _ or space or a tab with '', then it will replace all tabs with \r. Why did I include tabs, well the asker specifies excel data.

Good Luck!!

Update for your comments:

if (txt.match(/[^1-9_\t ]/g)) {
    alert('error');
} else {
    txt=txt.replace(/[\t]/g,'\r');
}
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • am not going to replace anything gitaar,the text area should have values (1-9 and _ and space) if anything else apart from these have to throw an alert.. if text is correct then we need to delimit the value with space or '\r' – jcrshankar Aug 18 '12 at 06:00
  • That was unclear in your question, and you didn't mention the space. Then use the first regex to `.match()` for the data you expect. Use the second regex to delimit your values with `\r` or `' '` (what ever you choose!). – GitaarLAB Aug 18 '12 at 06:03
  • You'd need to assign the result of the replacement back into some variable, or otherwise do something with it. – nnnnnn Aug 18 '12 at 06:12
  • Gitaar thanks a lot, if no error alert,am going to delimit text area with '\r' and have to dind the count. – jcrshankar Aug 18 '12 at 06:54
  • if (input.match(/[a-zA-Z]/) && input.match(/[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]/)) .. this is the way i avoid the un wanted characters is there is any other way,sure. – jcrshankar Aug 18 '12 at 07:09
  • I showed you the other way a number of times (in fact it is still in my answer), giving you working and correct answers based on your specified requirements. If you continuously keep changing your requirements (of characters you want to exclude) I can't keep on helping you. If you believe your regex is correct, then use it! (I don't believe the last one is correct, for instance you should escape special regex characters like `[` when this is needed, but after your last comment it is even more unclear what you want EXACTLY.) I don't mean this in a rude way. – GitaarLAB Aug 18 '12 at 07:24
  • 1
    @jcrshankar - If you have a short-list of _allowed_ characters then your regex should be based on those characters as in this answer. Don't try to list every possible "error" character. The regex from your last comment doesn't mention ¡ or ¿ for example, or « or © - are they allowed? – nnnnnn Aug 18 '12 at 07:35
  • http://stackoverflow.com/questions/12032531/how-to-clear-the-textarea-in-onpaste-method , i come up with like this thanks gitaar and nnnnnn. – jcrshankar Aug 20 '12 at 05:18