开发者

RegEx: Validating Alternate text for image

开发者 https://www.devze.com 2023-01-07 00:28 出处:网络
I\'m trying to set validation for an image al开发者_如何学运维ternate text, and here\'s what I think should be validated so far. It\'s a pretty simple RegEx, but I\'m yet to start learning that topic.

I'm trying to set validation for an image al开发者_如何学运维ternate text, and here's what I think should be validated so far. It's a pretty simple RegEx, but I'm yet to start learning that topic..

  • Double quotes
  • < and > characters to prevent HTML input

Is there anything else you would add to this? Would text length ever be an issue?

I appreciate your help and if someone could provide this simple RegEx I'd be really grateful :)


That sounds like a good place to start for me. Max size: pick something sane, unless you want it to be valid to post a dissertation as an alt text - though it is probably possible. As for the regex to validate it's okay:

/^[^"<>&\\]{0,XXX}$/

where XXX is the maximum size you want. Or get rid of the {0,XXX} altogether and replace it with * to mean "zero or more". Syntax depends on language, of course.

Also found this, looked interesting:

http://www.cs.tut.fi/~jkorpela/html/alt.html

Update:

Yeah, you two make a good point. As long as the quotes used around the alt-text aren't themselves single-quotes, then they should be fine.

And as per other answers below, possibly also & and . Though you may need to be careful with how many slashes, whether they are before things that matter. And also, whether   and such things are allowed in the text itself.

0

精彩评论

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