admin管理员组文章数量:1414605
export default (value = "") => {
let error = null;
const stripped = value.replace(/^[0-9a-zA-Z]*$/, "");
if (stripped.length !== value.length) {
error = "Input Contains Bad Characters";
}
return { error, value };
};
// This below code for varying the formats
import validator from "../../validators/alpha-numeric.js";
describe("Alphanumeric Only Validator For Input Validation", () => {
test("Only Characters Passed", () => {
expect(validator("Abcd")).toMatchObject({
error: null,
value: "Abcd"
});
});
test("Alphanumeric Characters With Space", () => {
expect(validator("Abc d")).toMatchObject({
error: "Input Contains Bad Characters",
value: "Abc d"
});
});
test("Alphanumeric Characters With Numbers", () => {
expect(validator("Ab123d")).toMatchObject({
error: null,
value: "Ab123D"
});
});
test("Alphanumeric Characters With Special Characters", () => {
expect(validator("Abcd@")).toMatchObject({
error: "Input Contains Bad Characters",
value: "Abcd@"
});
});
});
export default (value = "") => {
let error = null;
const stripped = value.replace(/^[0-9a-zA-Z]*$/, "");
if (stripped.length !== value.length) {
error = "Input Contains Bad Characters";
}
return { error, value };
};
// This below code for varying the formats
import validator from "../../validators/alpha-numeric.js";
describe("Alphanumeric Only Validator For Input Validation", () => {
test("Only Characters Passed", () => {
expect(validator("Abcd")).toMatchObject({
error: null,
value: "Abcd"
});
});
test("Alphanumeric Characters With Space", () => {
expect(validator("Abc d")).toMatchObject({
error: "Input Contains Bad Characters",
value: "Abc d"
});
});
test("Alphanumeric Characters With Numbers", () => {
expect(validator("Ab123d")).toMatchObject({
error: null,
value: "Ab123D"
});
});
test("Alphanumeric Characters With Special Characters", () => {
expect(validator("Abcd@")).toMatchObject({
error: "Input Contains Bad Characters",
value: "Abcd@"
});
});
});
This is full code i am using in React, please guide me the perfect result how can i verify alphanumeric.
This is the requirement even the below describe code i have written that is for unit testing, but if i am running "yarn unit-test" so i am getting error.
Share Improve this question asked Jan 10, 2019 at 10:50 Sanjay TiwariSanjay Tiwari 1331 gold badge2 silver badges9 bronze badges2 Answers
Reset to default 2This:
const stripped = value.replace(/^[0-9a-zA-Z]*$/, "");
Only replaces those characters if they're the only ones in the string, because of the anchors (^
and $
).
If you want to check value
only contains those characters, there's no need to create another string:
if (/[^0-9a-zA-Z]/.test(value)) {
// It has an invalid character
}
That works because the regular expression will match any character not in 0-9, a-z, or A-Z. If it doesn't match, there aren't any invalid characters.
Example:
function test(value) {
if (/[^0-9a-zA-Z]/.test(value)) {
console.log(JSON.stringify(value), "=> invalid");
} else {
console.log(JSON.stringify(value), "=> valid");
}
}
test(""); // Valid
test("0a"); // Valid
test("Ab123d") // Valid
test("0a-"); // Invalid
Hello everyone i have got the solution of this question, please find the alphanumeric pattern /\w/
. This pattern is working for me, also please find the steps for regex creation...enter link description here
本文标签: javascriptReact alphanumeric validation issueStack Overflow
版权声明:本文标题:javascript - React alphanumeric validation issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745194220a2647058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论