2.5 Specialized Validation
Making sure data has the right type can get you pretty far. But what about specialized cases like email addresses that require a specific format? In this lesson, we will cover how to handle these types of validations using regular expressions.
1.Introduction3 lessons, 15:15
2.Handling Form Data5 lessons, 36:42
3.Conclusion1 lesson, 01:21
2.5 Specialized Validation
The final type of validation that I wanna take you through has to do with the format of the data that's being passed in. So in a lot of cases, for the most part, freeform data is gonna be okay, maybe your name, maybe some comments, maybe other things like that. But what if the input that you want to get from the user needs to have a specific format and what I'm speaking of specifically at this point is an e-mail. So you know that emails kind of come in certain formats where you have some combination of letters and numbers and then there's an @ symbol and there's some combination of letters and numbers and then a period and then an extension like a .com, a .net, something like that. Now what I want to show you in this lesson is how you can go about doing specific format validations for certain things. So in this case what I wanna do is I wanna create another function, since I'm not really talking about required any more. So at this point I'm going to say, validate_input_format, something like that. We're gonna pass in the input value, once again. We're gonna pass in the input name, once again, and at this point, we can do this a couple of different ways. And I'm gonna pass in the expected format. So what I'm gonna do here is I'm gonna use a trick to have PHP validate the input using a regular expression. And if it matches, then it's going to say yep, that is good, you're good to go. And if it doesn't match, then it's going to give out an error. So this is gonna be pretty simple. What we're gonna do here is we're simply going to return. We want to use another function called preg or preg match, which is going to take in a couple of parameters. The first one is gonna be the expected pattern. Now what I'm gonna drop in here is pretty ugly and nasty. And if you know anything about regular expressions, you'll be able to look at this very quickly, and kind of get a picture as to what it means. And if you don't, don't really worry about it. All I really did was search around the Internet and found a relatively generic regular expression that can represent most email addresses. So let's go ahead and drop that in there. So basically what this is saying, as I was mentioning before, is there's some combination of letters and numbers. After that, there's an @ symbol. Then there's a domain here, so this is gonna be some combination of letters and numbers. There's gonna be a period, and then, once again, some sort of combination of letters and numbers here. Typically, just letters, but this should kinda get the job done. Now the second parameter that I'm passing in here is going to be the actual value that I am searching in to find this, and this is gonna be my input value. And what's gonna happen here is if this matches, if we do find this format to be found within this input value, then this is going to return a truthy value. And if it doesn't, then we can go ahead and assume that it didn't. So if this did find a match, then we're simply gonna leave it alone and we're gonna say there's nothing that we care about there. But if it doesn't, then in this case, we need to know that this is not in the correct format. So we'll say, in this case, the input name, Was not in the correct format, just like that. And that's all we're gonna return. So now we have this new function here, so the next thing that I'm gonna have to do is put down here another one of these spans into, copy this down into our email here. So let's go ahead and drop this in here. But this is not required so I'm not gonna put a little star in there. In this case, this is going to be an email error. So we'll come up to the top here, and we'll bring in, or create, a new email error variable, bring that into the mix. And now, I can come in here and say that emailError is gonna be equal to, and in this case, I want to validate the input format. And I can go ahead and say that this is going to be whatever we got for email, this is going to be for email. And then this is actually where we are gonna drop that regular expression, so I don't have to put this in here necessarily like this. So I'm gonna cut this, bring this up here into what we're passing in. And then down here all I'm really doing is checking for that expected format, there we go. So let's go ahead and save all of this, and it looks like I forgot a semicolon. There we go, save. All right, so if we've typed in everything correctly, we go ahead and we go ahead and refresh those pages, everything should look the same. And if I hit submit, you're gonna see now that we have name is required, comment is required, and email was not in the correct format. So if I were to come in here and say, firstname.lastname@example.org and hit submit, this is going to go away cuz you're gonna see that at the proper format. But if I were to just type in here a couple of d's and go ahead and hit submit, then once again, that is not following, or matching, on that regular expression that we were checking against. And now we can say that the email was not in the correct format. And then obviously if you've had any other type of inputs, maybe say a website URL or something else that is basically gonna follow some sort of kind of predefined formula, we can start to check for those things and let the user know that, hey, this was not in the right format. You need to fix the input for this one. So that's a very simple way that you can start to add in some format validation.