0

ami 私はここで何か間違ったことをしていますか? 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 に答える