我正在尝试使用正则表达式来验证javascript中的电话号码

时间:2019-08-26 14:35:46

标签: javascript regex

我试图弄清楚为什么这行不通。我需要以某种形式验证电话号码,而我的研究表明这应该适用于北美电话号码格式。

我最后有/ g,但是仍然无法正常工作……正确格式化的输入仍然是错误的。

我使用了https://regex101.com/,看起来像是对的!

<!DOCTYPE html>
<html lang="en-US">

<head>
</head>

<body>  
    <section>
        <form>        
            <label for="phoneNumber">Phone Number
            </label>
            <input type="text" name="telephone" placeholder="Enter your Phone Number" />
        </form>
    </section>  
let telInput = form.querySelector("[name='telephone']"),
    tel = telInput.value;
//regex solution:
regex = /\([0-9]{3}\)\n[0-9]{3}-[0-9]{4}/;
if (!regex.test(tel)) {
        alert("You have to use (xxx) xxx-xxxx format.");
    telInput.focus();
        return;
    }

1 个答案:

答案 0 :(得分:1)

您的正则表达式非常接近!尝试用 image = Image.create(workspace = az_ws, name=resolve_image_name(), models=[model], image_config = image_config) image.wait_for_creation() print("created image") if(image.creation_state != "Succeeded"): raise Exception("Failed to create image.") print("image location: {}".format(image.image_location)) artifacts = {"image_location" : image.image_location} if(not os.path.exists("/artifacts/")): os.makedirs("/artifacts/") with open("/artifacts/artifacts.json", "w") as outjson: json.dump(artifacts, outjson) (空格文字)替换\n(换行符)。添加锚点还可以帮助您避免部分匹配。

^$

Try it here!