0

我在这里做错了什么?我正在尝试使用 javascript 运行一小段验证 bny。我正确地做正则表达式吗?

var z=document.forms["myForm"]["website"].value;
if (z==null || z=="")
  {
  $('.five').show();
  return false;
  }
var validWebsite = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
if(!validWebsite.test(document.forms["myForm"]["website"].value))
{
  $('.five').html('Enter a Valid Website');
  return false;
}

var number = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/
if(!number.test(document.forms["myForm"]["number"].value)){
    $('.six').show()
    return false;
}

html

*email: <br>
<input type="text" name="email"/><div class="four alertmessage" style="display:none;color:red;">Enter your Email</div><br>
website: <br>
<input type="text" name="website"/><div class="five alertmessage" style="display:none;color:red;">Enter your Website</div><br>
Contact phone number: <br>
<input type="text" name="number"/><div class="six alertmessage" style="display:none;color:red;">Enter your Number</div>

<input name="submit" class="submitButton" type="submit" value="Submit">
4

1 回答 1

0
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function validate()
{
    var z=document.forms["myForm"]["website"].value;
    if (z=="")
    {
        $('.five').show();
        return false;
    }

    var validWebsite = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    if(!validWebsite.test(z)){
        $('.five').html('Enter a Valid Website');
        return false;
    }

    var n=document.forms["myForm"]["number"].value;
    var number = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/;
    if(!number.test(n)){
    $('.six').show()
    return false;
    }

    return true;
}
</script>

<form onsubmit="return validate()" method="post" name="myForm"> 
*email: <br>
<input type="text" name="email"/><div class="four alertmessage" style="display:none;color:red;">Enter your Email</div><br>
website: <br>
<input type="text" name="website"/><div class="five alertmessage" style="display:none;color:red;">Enter your Website</div><br>
Contact phone number: <br>
<input type="text" name="number"/><div class="six alertmessage" style="display:none;color:red;">Enter your Number</div>

<input name="submit" class="submitButton" type="submit" value="Submit">
</form>
于 2013-02-23T07:05:36.287 回答