开发者

what is the logic for paragraph validation in multiline Textbox using javascript?

i am trying to validate text(Paragraph) in javascript for multiline Textbox. After giving the text in textbox, the very first Letter should be changed to Capital letter, remaining have to remain as small letters.After full stop the first letter should be in Capital. I need to use textchange event also.

I am new to javascript, and i feel this Proper Case validation is very co开发者_开发百科mplex, i am not even getting any logic to start with.. Plz give some idea.

Thanks in advance.


Here's a possible answer, though to reiterate my comment, I don't actually think this is a very useful validation approach. You can see a working version here: http://jsfiddle.net/nrabinowitz/5TSK5/

// regex for a single sentence
var testRE = /^[A-Z][^A-Z]+$/;

var paragraphs = text.split('\n');

var pass, paragraph, sentences, sentence;
for (var x=0; x < paragraphs.length; x++) {
    paragraph = paragraphs[x];
    pass = true;
    if (paragraph) {
        sentences = paragraph.split(/\. +/);
        for (var y=0; y < sentences.length; y++) {
            sentence = sentences[y];
            // test sentence for validity
            if (sentence && !testRE.exec(sentence)) {
                pass = false;  
            }
        }
        // pass is now either true or false for paragraph x
    }
}


You might want to look for a plugin that already does grammar checking like After The Deadline.

@nrabinowitz is right, I wouldn't try to write a natural language grammar checker in javascript unless your stated goal of "capitalization should only occur after a full stop" is the full extent of what you want to do, you'd be attempting to parse a potentially non-context-free language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜