0

I'm currently writing a function that would check for the value of each line in a textarea and output the results in another textarea.

My problem now is that I don't know the value of a blank line in a text area, I've tried "", /\W/, undefined, and null, and none of those seem to work. This is what I have right now:

function check(){
            var textAreaValue= $("textarea#minCheck").val();
            var valueArray= $("textarea#minCheck").val().split("\n");
            var parsedArray=[];

            for(i=0; i<valueArray.length;i++){
                if(valueArray[i]==/\W/){
                    parsedArray.push(valueArray[i] + "|blank");
                }else if(valueArray[i]>=0 && valueArray[i]<=999){
                    parsedArray.push(valueArray[i] + "|" + "brand1");
                }else if(valueArray[i]>=1000 && valueArray[i]<=1999){
                    parsedArray.push(valueArray[i] + "|" + "brand2");
                }else if(valueArray[i]>=2000 && valueArray[i]<=2999){
                    parsedArray.push(valueArray[i] + "|" + "brand3");
                }else if(valueArray[i]>=3000 && valueArray[i]<=3999){
                    parsedArray.push(valueArray[i] + "|" + "brand4");
                }else{
                    parsedArray.push(valueArray[i]+"|invalid");
                }
            }

            $("textarea#minParsed").val(parsedArray.join("\n"));
        }

For now, it gives me "|brand1" when a blank line is checked.

wong1
  • 59
  • 1
  • 6
  • 1
    `valueArray[i]==/\W/` won't check if the content of `valueArray[i]` matches the regexp `/\W/`. It will compare the two objects, but because the `valueArray[i]` will be a string and the `/\W/` and `RegExp` this will never be true. You would need to use the [test()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) function of a RegExp. – t.niese Aug 31 '15 at 05:27

3 Answers3

1

try this : trim the value of line and check with empty space if(valueArray[i].trim()==""){, see below code

     function check(){
        var textAreaValue= $("textarea#minCheck").val();
        var valueArray= $("textarea#minCheck").val().split("\n");
        var parsedArray=[];

        for(i=0; i<valueArray.length;i++){
            if(valueArray[i].trim()==""){
                parsedArray.push(valueArray[i] + "|blank");
            }else if(valueArray[i]>=0 && valueArray[i]<=999){
                parsedArray.push(valueArray[i] + "|" + "brand1");
            }else if(valueArray[i]>=1000 && valueArray[i]<=1999){
                parsedArray.push(valueArray[i] + "|" + "brand2");
            }else if(valueArray[i]>=2000 && valueArray[i]<=2999){
                parsedArray.push(valueArray[i] + "|" + "brand3");
            }else if(valueArray[i]>=3000 && valueArray[i]<=3999){
                parsedArray.push(valueArray[i] + "|" + "brand4");
            }else{
                parsedArray.push(valueArray[i]+"|invalid");
            }
        }

        $("textarea#minParsed").val(parsedArray.join("\n"));
    }
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
1

You can check blank line using this regex expression : /^\s*\n/gm and for Mac line endings use: /^\s*[\r\n]/gm

Nesh
  • 134
  • 9
  • 1
    The `[\r\n]` is not wrong, but the explanation. The Line endings of OS X and Linux is `\n`. On windows it is `\r`. Only for MacOS prior to OS X it was `\r\n`, but `[\r\n]` would not match those because `[\r\n]` is either `\r` or `\n`. – t.niese Aug 31 '15 at 05:35
1

It seems like you are trying to use regular expression metacharacters (\W) without a Regexp call.

The literal value is probably \r\n (CR+LF) so if you split on \n you are left with \r

Try splitting on \r\n and then check for empty string ""

The HTML5 standard actually changes some things related to this, though I don't know what practical impact it has had in actual browser implementations. I think the HTML5 standard says that the client-side API could see any combination (CR, LF, or CR+LF), since the user is allowed to edit the content.

A better answer is here that describes the scenarios:

https://stackoverflow.com/a/14217315/257090

Community
  • 1
  • 1
codenheim
  • 20,467
  • 1
  • 59
  • 80