Mathieu
Mathieu

Reputation: 4787

Test that string begins with a specific substring

I am using validates_format with to check that a string begins with the following characters:

data:image/jpeg;base64

Here is a typical string I want to test:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigkJyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wAARCAAKAB4DAREAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAABgUH/8QAIxAAAgEEAQQDAQAAAAAAAAAAAQIDAAQFERMhIjGBBhIyQf/EABYBAQEBAAAAAAAAAAAAAAAAAAQCA//EACARAAICAgEFAQAAAAAAAAAAAAABAhEEIQMSEyIxYdH/2gAMAwEAAhEDEQA/AM4s7JWhdpEil5YyIzz/AFMTAjqR/em+lWkQyjjLQRXHG/1JU62DsGncS0D5XsXY2aHG/IYXkZIxLZyJCza1ye/HSiZqeqE4LW0w1lbVLbJXADhllIlCkdy7359AH3UcEvGjTIjUrDEX6q4kSL+L/a06HoDP2XMuo5sC2hs3DAnXnujoOS31tfP0bjJdtP6G5GaTMXpdixMr7JO991HTqKoS1cnZ/9k=

My code is:

validates_format_of :imagebase64,  :with => %r{ \A(data:image/jpeg;base64) }i, :message => "is a invalid data uri base64 file"

and it's not working. How can I add something to the regexp so that it rejects if the string has any white space?

Upvotes: 0

Views: 85

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

You may use

:with => /\Adata:image\/jpeg;base64\S*\z/i

where \S* matches zero or more non-whitespace chars and \z matches the end of string.

See the Rubular demo.

Upvotes: 1

Related Questions