diff --git a/src/locales/en.json b/src/locales/en.json index e959a4d9..8456d929 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -1137,6 +1137,7 @@ "Placement service:": "Placement service:", "Platform Info": "Platform Info", "Please confirm your password!": "Please confirm your password!", + "Please enter a valid ASCII code": "Please enter a valid ASCII code", "Please enter a valid Email Address!": "Please enter a valid Email Address!", "Please enter a valid Phone Number": "Please enter a valid Phone Number", "Please enter complete key value!": "Please enter complete key value!", diff --git a/src/locales/zh.json b/src/locales/zh.json index eba79604..8ab18170 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -1137,6 +1137,7 @@ "Placement service:": "放置服务(placement):", "Platform Info": "平台概况", "Please confirm your password!": "请确认您的密码", + "Please enter a valid ASCII code": "请输入有效的ASCII码", "Please enter a valid Email Address!": "请输入一个有效的邮箱地址", "Please enter a valid Phone Number": "请输入一个有效的手机号", "Please enter complete key value!": "请输入完整的键值!", diff --git a/src/pages/compute/containers/Instance/actions/StepCreate/SystemStep/index.jsx b/src/pages/compute/containers/Instance/actions/StepCreate/SystemStep/index.jsx index e9a4ac3f..2b61dd4e 100644 --- a/src/pages/compute/containers/Instance/actions/StepCreate/SystemStep/index.jsx +++ b/src/pages/compute/containers/Instance/actions/StepCreate/SystemStep/index.jsx @@ -19,7 +19,7 @@ import globalHypervisorStore from 'stores/nova/hypervisor'; import globalServerGroupStore from 'stores/nova/server-group'; import policyType from 'resources/server-group'; import Base from 'components/Form'; -import { getPasswordOtherRule } from 'utils/validate'; +import { getPasswordOtherRule, asciiValidator } from 'utils/validate'; import { hypervisorColumns, hypervisorFilters } from 'resources/hypervisor'; import { physicalNodeTypes } from 'resources/instance'; @@ -330,6 +330,7 @@ export class SystemStep extends Base { name: 'userData', label: t('User Data'), type: 'textarea-from-file', + validator: asciiValidator, hidden: !more, extra: t( 'The user needs to ensure that the input is a shell script that can run completely and normally.' diff --git a/src/utils/validate.js b/src/utils/validate.js index 24437622..cba44d52 100644 --- a/src/utils/validate.js +++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(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*\/(1[01][0-9]|12[0-8]|[0-9]{1,2})$/; +const asciiRegex = /^[\x00-\x7f]*$/; // eslint-disable-line export const regex = { cidr, @@ -71,6 +72,7 @@ export const regex = { imageNameRegex, instanceNameRegex, ipv6CidrOnly, + asciiRegex, }; export const isPhoneNumber = (value) => phone(value).isValid; @@ -335,6 +337,8 @@ export const macAddressMessage = t( 'Invalid Mac Address. Please Use ":" as separator.' ); +const asciiMessage = t('Please enter a valid ASCII code'); + export const phoneNumberValidate = (rule, value) => { if (!rule.required && !value) { return Promise.resolve(true); @@ -588,3 +592,10 @@ export const jsonValidator = (item, value) => { } return Promise.resolve(true); }; + +export const asciiValidator = (rule, value) => { + if (asciiRegex.test(value)) { + return Promise.resolve(true); + } + return Promise.reject(new Error(`${t('Invalid: ')}${asciiMessage}`)); +};