{"record":{"id":"659c8301a10523a0","repo":"dotnet/aspnetcore","slug":"invalid-number-value-attributevalue-for-param","errorCode":null,"errorMessage":"Invalid number value '${attributeValue}' for parameter '${parameterName}'","messagePattern":"Invalid number value '(.+?)' for parameter '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/CustomElements/src/js/BlazorCustomElements.ts","lineNumber":125,"sourceCode":"  static parseAttributeValue(attributeValue: string, type: JSComponentParameterType, parameterName: string): any {\n    switch (type) {\n      case 'string':\n        return attributeValue;\n      case 'boolean':\n        switch (attributeValue) {\n          case 'true':\n          case 'True':\n            return true;\n          case 'false':\n          case 'False':\n            return false;\n          default:\n            throw new Error(`Invalid boolean value '${attributeValue}' for parameter '${parameterName}'`);\n        }\n      case 'number':\n        const number = Number(attributeValue);\n        if (Number.isNaN(number)) {\n          throw new Error(`Invalid number value '${attributeValue}' for parameter '${parameterName}'`);\n        } else {\n          return number;\n        }\n      case 'boolean?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'boolean', parameterName) : null;\n      case 'number?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'number', parameterName) : null;\n      case 'object':\n        throw new Error(`The parameter '${parameterName}' accepts a complex-typed object so it cannot be set using an attribute. Try setting it as a element property instead.`);\n      default:\n        throw new Error(`Unknown type '${type}' for parameter '${parameterName}'`);\n    }\n  }\n}\n\nfunction dasherize(value: string): string {\n  return camelCase(value).replace(/([A-Z])/g, \"-$1\").toLowerCase();\n}","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/CustomElements/src/js/BlazorCustomElements.ts#L107-L143","documentation":"Thrown by parseAttributeValue when an attribute declared as type 'number' cannot be coerced via Number() — i.e. Number(attributeValue) returns NaN. Only strings that JavaScript's Number() can parse (e.g. '42', '3.14', '0x10', '1e3') are accepted; anything else aborts parameter application.","triggerScenarios":"Setting a numeric [Parameter] via an HTML attribute with a non-numeric string: '', 'abc', 'N/A', '1,000' (comma), localized decimals using comma as separator, or trailing whitespace that Number() rejects.","commonSituations":"Localization where thousands/decimal separators differ; binding a string UI field directly to a numeric parameter without coercion; empty default attributes; copying values from labels rather than inputs.","solutions":["Ensure the attribute string is a JavaScript-parseable number (no thousands separators, '.' as decimal point).","Pre-coerce the value before setting the attribute: String(Number(rawValue)) or a sanitization step.","Use a 'number?' parameter if an empty/unset value is valid so empty maps to null instead of throwing.","Strip non-numeric characters (commas, currency symbols) before assignment."],"exampleFix":"// before\nel.setAttribute('count', '1,000'); // throws\n\n// after\nel.setAttribute('count', String(Number(raw.replace(/[^0-9.]/g, ''))));","handlingStrategy":"validation","validationCode":"// Validate/coerce numeric attribute strings before assignment.\nfunction toNumberAttr(value) {\n  const cleaned = String(value).replace(/[^0-9.\\-eE]/g, '');\n  const n = Number(cleaned);\n  if (Number.isNaN(n)) throw new Error(`Not a number: ${value}`);\n  return String(n);\n}\nel.setAttribute('count', toNumberAttr(rawValue));","typeGuard":"function isValidNumberAttr(v) {\n  return v !== '' && v != null && !Number.isNaN(Number(v));\n}","tryCatchPattern":"try {\n  el.setAttribute('count', raw);\n} catch (e) {\n  if (/Invalid number value/.test(e.message)) {\n    el.setAttribute('count', String(Number(raw.replace(/[^0-9.\\-]/g, '')) || 0));\n  } else throw e;\n}","preventionTips":["Strip thousands separators and currency symbols before assignment.","Use 'number?' for nullable numeric parameters so empty maps to null.","Validate user input as a number before binding to a numeric attribute.","Localize decimal display only after coercion, not at the attribute layer."],"tags":["blazor-custom-elements","attribute-parsing","validation","number"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}