I want to validate a string like:
“v=spf1 a -all”
but it should also b possible to fill in a numeric value or something lik mail.website.com. It’s used for a dns manage tool.
For some reason this doenst seem to work. I think i am doing something wrong with the delimiters but i dont see what i have to change to let this work.
That’s because when starting with a character, regex is only valid until that character next appears undelimited. That’s why I recommend using ~ rather than / :
preg_match will return 1 if the pattern matches the subject string or 0 if that pattern did not match (or false if there’s a problem). Since you want to make sure that the string only contains characters in that specific list there are a few approaches.
Check that the whole string contains only the letters that you want
Note: both of these will allow an empty string (“”); you didn’t say whether that should be allowed or not. If you let us know which approach you like best (it doesn’t have to be one of the above) then we can help explain it in detail if you’re not really sure what is going on.
Looks great i have googled a litle bit on the /^and the /i and I understand what its doing now. I have used the second version and added a space else he wouldn’t validate it.
What would i have to change to only allow a / in the check. I have tried some version but i receive errors when i add the / or a //.
The string i want to validate is:
“v=spf1 a -all” (quotes included)
But it must also allow something like the word mail or a ip adress. So it should check if there arent any othe characters in the string the things like a-z0-9 and " = - .