Javascript - Kiểm tra xem có đúng tên miền?
Viết một hàm JavaScript để kiểm tra xem một chuỗi có phải domain.
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Write a JavaScript function to check whether a given value represents a domain or not</title>
<script>
function is_domain(str)
{
regexp = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/i;
if (regexp.test(str))
{
return true;
}
else
{
return false;
}
}
document.write(is_domain('www.example.com')+"<br/>");
document.write((is_domain('www.npm.co.uk')+"<br/>");
document.write((is_domain('http://www.example.com')+"<br/>");
document.write((is_domain('https://www.example.com')+"<br/>");
document.write(is_domain('www.example.com'));
</script>
</head>
<body>
</body>
</html>
Lưu đồ thuật toán: