简体   繁体   English

javaScript检查字符串中至少一个字母数字字符

[英]javaScript check for at least one alphanumeric character in string

In my application someone can submit text from another language. 在我的应用程序中,有人可以用其他语言提交文本。 I'd like for only English alphanumeric strings to be entered. 我只想输入英文字母数字字符串。 I've got this JavaScript function working but wondering if this is the best method for doing this? 我已经使此JavaScript函数正常工作,但想知道这是否是执行此操作的最佳方法吗?

var string = $('input[name=title]').val();
if((/\d/.test(string) || /[a-zA-Z]/.test(string)) === false) {
    alert('Field has no Alphanumeric characters.');
    fPass = false;
}

Even if some just enters 1 character I want that to be allowed, as long as it is a number or a character from one of the 26 letters in the English alphabet (case insensitive) 即使有人只输入1个字符,我也希望它被允许,只要它是数字或英语字母表中26个字母之一中的一个字符(不区分大小写)

Without using a function here is what I've come up with 在不使用功能的情况下,我想到了

if((/[\da-z]/i.test(string)) === false) {
    alert('Please use alphanumeric characters.')
}

You can combine your regex into one expression. 您可以将正则表达式合并为一个表达式。

function hasAlphanumeric(str) {
  return /\d|[A-z]/.test(str)
}

You can use 您可以使用

^[\da-z]+$

在此处输入图片说明


 let allowAlphanumeric = (str) =>{ return /^[\\da-z]+$/i.test(str) } console.log(allowAlphanumeric('$#@')) console.log(allowAlphanumeric('')) console.log(allowAlphanumeric(' ')) console.log(allowAlphanumeric('abchbchdb12e44')) 

暂无
暂无

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

相关问题 在javascript中检查字符串是否为字母数字 - To check if a string is alphanumeric in javascript 正则表达式“密码必须至少包含一个非字母数字字符” - Regex for “password must contain at least one non-alphanumeric character” 包含至少1个数字和1个字符且固定长度为11的字母数字字符串的正则表达式 - Regex for alphanumeric string with at least 1 number and 1 character and a fixed length of 11 JavaScript 如何检查字符串是否至少包含一个字母? - How to check if a string contains at least one letter in JavaScript? javascript正则表达式要求至少一个特殊字符 - javascript regex to require at least one special character 如何检查字符串是否包含至少一个数字,字母和既不是数字也不是字母的字符? - How do I check if a string contains at least one number, letter, and character that is neither a number or letter? 检查字符串变量是单个字母数字字符的最有效方法 - Most efficient way to check that string variable is a single alphanumeric character 正则表达式为字母数字密码,至少包含1个数字和字符 - Regex for alphanumeric password, with at least 1 number and character 是否在JavaScript中找到最长的无任何数字的子字符串以及至少一个大写字符? - Find longest sub-string without any number and at least one upper case character in JavaScript? 正则表达式测试至少一个数字,可以是字母数字,并且应允许一些特殊字符 - Regex to test at least one number,can be alphanumeric,and should allow some special character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM