简体   繁体   English

如何验证此字符串在Javascript中不包含任何特殊字符?

[英]How to validate this string not to contain any special characters in Javascript?

I have a dynamic string like this which may look like: 我有一个像这样的动态字符串,看起来像:

"69.43.202.97"                 OR               "ngs.pradhi.com"

I want to validate these string contains only numbers, english alphabets and "." 我想验证这些字符串是否仅包含数字,英文字母和“。” . I want to validate this in front end using java script. 我想使用Java脚本在前端进行验证。 I thought of using regular expression like this: 我想到使用这样的正则表达式:

function validatePath() {
    var path = document.getElementById('server_path').value;
    path.match([a-z][0-9])  //or something like this
}

If the path is invalid despite of displaying the alert box I just want to show the error below the text field as soon as the user fills the server path. 如果尽管显示了警告框,但该路径仍然无效,我只想在用户填写服务器路径后立即在文本字段下方显示错误。 How can I do that? 我怎样才能做到这一点?

My full javascript function looks like this: 我完整的javascript函数如下所示:

function validatePath() {
    var path = document.getElementById('server_path').value;
    if (path.search(":") == -1){
        alert("Invalid server path");
    }
    else{
        var host_name = path.split(":")[0]
        if host_name.match("^[a-zA-Z0-9.]*$")) {}

    }

} }

try path.match("^[a-zA-Z0-9.]*$") 尝试path.match("^[a-zA-Z0-9.]*$")

EDIT: var regex = new RegExp("^[a-zA-Z0-9.]*$"); if(regex.test(host_name)){} 编辑: var regex = new RegExp("^[a-zA-Z0-9.]*$"); if(regex.test(host_name)){} var regex = new RegExp("^[a-zA-Z0-9.]*$"); if(regex.test(host_name)){}

您可以使用/^[a-zA-Z0-9.]*$/.test(path) ,它将返回true或false

不同的是,替换方法:

if(path.replace(/^[a-z\d.]*$/i,"")=="")

You can do the check (including port presence) with a regex like this: 您可以使用以下正则表达式进行检查(包括端口是否存在):

^[a-zA-Z0-9\.]+:[0-9]+$

(Note the + instead of * to account for empty path or port (they are not allowed).) (请注意,用+而不是*表示空路径或端口(不允许使用)。)

If you are using HTML5, consider the newly introduced "pattern" and "required" attributes, which can posdibly save you some JS code. 如果您使用的是HTML5,请考虑新引入的“ pattern”和“ required”属性,这些属性有可能为您节省一些JS代码。

See this short demo for an illustration of both technics. 有关这两种技术的说明,请参见此简短演示

Some links you might find useful: 您可能会发现一些有用的链接:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript修改数组中的字符串以包含特殊字符 - How to modify a string in an array to contain special characters using javascript 正则表达式验证字符串是否包含不超过特殊字符数的特殊字符 - Regex validate string if it does not contain more than special count of special characters 向javascript函数提供可能包含特殊字符的字符串参数 - Supplying string parameters that may contain special characters to a javascript function JavaScript字符串中的特殊字符 - Special characters in javascript string 如何验证电话号码不包含任何其他字符,包括 - 和。 在 html 页面中使用 javascript,不应少于 10 位 - How to validate a phone number doesn't contain any other characters including - and . using javascript in a html page, shouldnt be less than 10 digits 如何在 Javascript 中忽略字符串排序中的特殊字符 - How to ignore special characters in string sorting in Javascript 有没有办法使用JavaScript在2个不同的特殊字符中提取字符串? - Any way to extract string within 2 different special characters using javascript? perl,javascript - 如何将 perl 中打印的任何特殊字符转义为 ZDE9B9ED78D7E2E1DCEEFFEE780E2F9 - perl, javascript - how to backescape any special characters printing in perl into javascript Javascript Regex验证单个特殊字符的出现 - Javascript Regex to validate single occurrence of special characters javascript字符串替换特殊字符 - javascript string replace special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM