|
|
|
อยากได้ สคริปตรวจสอบรูปแบบเว็บไซด์ Check URL ให้ถูกต้อง โดยตรวจสอบจากฟอร์มรับข้อมูล |
|
|
|
|
|
|
|
Code
function validateURL()
{
lengthValue = Trim(document.getElementById(txtURL).value);
lengthValue = lengthValue.length;
if(lengthValue != 0)
{
var j = new RegExp();
j.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
lengthValue = Trim(document.getElementById('txtWeb').value);
if (!j.test(lengthValue))
{
alert(Please enter valid URL.");
return false;
}
}
|
|
|
|
|
Date :
2010-12-09 06:35:11 |
By :
th |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
function is_valid_url ( $url )
{
$url = @parse_url($url);
if ( ! $url) {
return false;
}
$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';
if ($path == '')
{
$path = '/';
}
$path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';
if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) )
{
if ( PHP_VERSION >= 5 )
{
$headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
}
else
{
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if ( ! $fp )
{
return false;
}
fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
$headers = fread ( $fp, 128 );
fclose ( $fp );
}
$headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
}
return false;
}
|
|
|
|
|
Date :
2010-12-09 06:35:55 |
By :
th |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอา Regula นี้ไปศึกษาแล้วดัดแปลงเอานะครับ
Code (PHP)
^(http|https|ftp)://(www\.)?[a-z][a-z0-9\_\-]*(\.[a-z[a-z0-9\_\-]*)+(/[a-z0-9\_\-]*\.[a-z]+)?$
วิธีใช้
Code (PHP)
$url="https://www.thaicreate.com";
$pattern="^(http|https|ftp)://(www\.)?[a-z][a-z0-9\_\-]*(\.[a-z[a-z0-9\_\-]*)+(/[a-z0-9\_\-]*\.[a-z]+)?$";
if(!ereg($pattern,$url,$regs)){
echo"URL ไม่ถูกต้อง";
}else{
echo"URL ถูกต้องที่สุด";
}
|
|
|
|
|
Date :
2010-12-09 08:44:00 |
By :
somparn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|