javascript验证表单输入网址

JavaScript025

javascript验证表单输入网址,第1张

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

</head>

<body>

<input onblur="text()" id="text" type="text">

<script type="text/javascript">

function text(){

var string1 = document.getElementById('text')

var string2 = string1.value.indexOf('http://')

if(string2 != 0)

{

alert('你输入的网址不正确')

return false

}

else alert('合法')

}

</script>

</body>

</html>

<!-- 试试,是这样的效果吗? -->

很简单,看代码:

<input name="" type="text" id="url" onblur="url(this)" value="" />

<script type="text/javascript">

function url(obj){

var path = obj.value

if( path == '' )

return

if( path.slice(0,7) == 'http://' )

path = path.slice(7)

path = path.split('/')

path = path[0]

obj.value= path

}

</script>

这个是鼠标失去聚焦,就判断。、