简体   繁体   中英

Comment Box like Twitter

What I am trying to achieve is something like comment box on Twitter web site. I want to set a perfect regular expression which can include URL format like htttp://www.abc.com, abc.com, http://abc.com and www.abc.com. This should reduce only 20 characters from total text field limit of 140 characters. After 140 characters user should not allowed to type in.

I have find a good option from github but it does not have all these functionality and I am trying to fix it but still there is no success. Here I have given same link for your reference. http://jsfiddle.net/FUADn/1/

What still not working for me is:

1) Not able to track urls like: "abc.com", "www.abc.com" and "http://abc.com"

2) Not able to restrict typing after 140 characters, It continues typing and shows text in red color

Can any of you help me with this please

About the first question you can try a regular expression like this one:

((http|https):\/\/)?(www\.)?([A-Za-z0-9$_.+!*\(\),;/?:@&~=-])+(\.[A-Za-z0-9]{2,})?(\.[A-Za-z0-9]{2,4})  

I am not sure if it will be enough for your case, but you can give it a try.

As for the second question you can use event.preventDefault() . For example:

$("#post-msg").keypress(function (e) {
    var a=$("#post-msg").val().length; 

    if (a > 140) {
        e.preventDefault();
    }
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM