Javascript and PHP verify that the URL entered by the user is correct.

Source: Internet
Author: User

Javascript and PHP verify that the URL entered by the user is correct.

The example in this article describes how to verify whether the URL address entered by the user is correct by using Javascript and PHP. we will share it with you for your reference. The specific method is as follows:

1. javascript checks URL address validity:

Copy codeThe Code is as follows: <script type = "text/javascript">
Function checkUrl (){
Var url = document. getElementById ('url'). value;
If (url = ''){
Alert ('url address cannot be blank ');
Return false;
} Else if (! IsURL (url )){
Alert ('url format should be http://www.bkjia.com ');
Return false;
} Else {
Return true;
}
Return false;
}
Function isURL (str)
{
// Varreg =/[0-9a-zA-z] +. (html | htm | shtml | jsp | asp | php | com | cn | net | com.cn | org) $ /;
// It must contain at least one character. (the last one has at least one character.) and at least one word character. The last character must be a word character or/
Varreg =/w +. (w + | w +/) $ /;
Varisurl = reg. test (str); // The test (str) method is a js correct expression. The built-in object can be directly called.
Returnisurl;
}
</Script>

<Form action = "" method = "post" onsubmit = "checkUrl ();">
URL:
<Input type = "text" name = "url" id = "url" value = "http://www.bkjia.com" onfocus = "this. value ='' "/>
<Br/>
<Input type = "submit" value = "get address" name = "get"/>
<Input type = "submit" value = "download" name = "download"/>
</Form>

2. PHP checks URL address Validity

Copy codeThe Code is as follows: <?
Function is_url ($ str ){
Return preg_match ("/^ http: // [A-Za-z0-9] +. [A-Za-z0-9] + [/=? % -&_~ '@ []': +!] * ([^ <> "]) * $/", $ Str );
}
?>

If you want to try a normal method, you can use the file_get_contents () function to verify it.

I hope this article will help you with PHP programming.


In php, how does one use javascript code to determine whether the entered verification code is correct?

Verification codes are generally randomly generated on the server. You can use ajax to obtain the verification code on the html page and compare it with the verification code entered by the user.
<Script type = "text/javascript">
Var xmlHttp;
Function createXMLHttpRequest ()
{
If (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();
}
}
Function doit ()
{
CreateXMLHttpRequest ();
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("get", "auth_code.php", true );
XmlHttp. send (null );
}
Function handleStateChange ()
{
If (xmlHttp. readyState = 4)
{
If (xmlHttp. status = 200 | xmlHttp. status = 0)
{
CheckAuthCodeInput (xmlHttp. responseText );
}
}
}
Function checkAuthCodeInput (authcode)
{
If (document. getElementById ("Custom"). value = auth)
{
Return true;
}
Return false;
}
</Script>

<A href = "document. getElementById ('img _ auth_code '). src = 'auth _ code. php? Code = '+ Math. random (); "> can't see clearly, change another one </a>

// This page is used to randomly obtain the verification code rand_auth_code.php
<? Php
Function get_auth_code ()
{
For ($ I = 0; $ I <5; $ I ++)
{
$ _ GLOBALS ['rand _ str']. = strtoupper (dechex (rand )));
Rand_str_width + = imagefontwidth ($ I );
}
}
Echo $ _ GLOBALS ['rand _ str'];
?>

<? Php
Include_once ("rand_auth_code.php"); // import the page that generates the verification code
$ Img_width = 100;
$ Img_height = 25;
$ Img = imagecreatetruecolor ($ img_width, $ img_height );

$ Img_bg_col ...... remaining full text>

Does PHP verify user input have different results from javaScript verify user input?

Is there a problem with connecting the page to the database? I suggest you change the method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.