សុពលភាព URL ដោយប្រើ Javascript

ជួនកាលយើងត្រូវការដើម្បីពិនិត្យមើលថាតើ URL ដែលបានបញ្ចូលដោយអ្នកប្រើនៅក្នុងប្រអប់អត្ថបទមួយគឺត្រឹមត្រូវឬយ៉ាងណា។
aខាងក្រោមកូដគំរូនៃរបៀបធ្វើ

The script

Add between your <head>..</head> tags.

<script type="text/javascript">// <![CDATA[
function checkUrl()
{
var theUrl=document.getElementById('url').value;
if(theUrl.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
theUrl.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i))
{
alert("valid Url");
}
else
{
alert("Not valid Url");
document.getElementById('url').focus();
return false;
}
}
// ]]></script>

The form to check

<form name="url_validate">
Enter URL: <input type="text" id="url" name="url" size="50" value="http://" />
<input type="button" onClick="checkUrl();" value="Validate" />
</form>

Full code

<html>
<head>
<script type="text/javascript">// <![CDATA[
function checkUrl()
{
var theUrl=document.getElementById('url').value;
if(theUrl.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
theUrl.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i))
{
alert("valid Url");
}
else
{
alert("Not valid Url");
document.getElementById('url').focus();
return false;
}
}
// ]]></script>
</head>
<body>
<form name="url_validate">
Enter URL: <input type="text" id="url" name="url" size="50" value="http://" />
<input type="button" onClick="checkUrl();" value="Validate" />
</form>
</body>
</html>

Break down of the script

var theUrl=document.getElementById('url').value;