I have a phone number input field that looks like this:
<input type="text" name="phone" maxlength="10" size="40" value="{$tmp.phone}"><>
Can I apply a code inside here? Or would I apply a function in an external .php file to do this?
What would the code look like?
I am new to all this coding stuff and need some help...
You can use the new HTML5 attribute pattern
. New browsers will not send the form until it is filled correct:
<form>
<input type="text" name="phone" pattern="^[0-9]{6}|[0-9]{8}|[0-9]{10}$" />
<input type="submit" />
</form>
If a user is using an older browser, the attribute is ignored. In this case you still should check it as Daniel and Pheonix recommended.
Even if get some way to put a check in HTML, always do validation on your server side. Client side Validations can always be by-passed.
in server side
$phone = $_GET['phone'];
if(!(strlen($phone)==6 ||strlen($phone)==8 ||strlen($phone)==10)){
//Error
}
For better user experience you can validate using javascript, and for security reasons is mandatory to use server side validation as Pheonix said.
<form id="form_id">
<input id="phone_id" type="text" />
<input type="submit"
onclick="if (document.getElementById('phone_id').value.length != 6
&& document.getElementById('phone_id').value.length!=8
&& document.getElementById('phone_id').value.length!=10)
{
alert('Error message...');
return false;
}" />
</form>
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.