Skip to content

fix: Validate country field accepting numeric input - #1714

Open
sashatakpere wants to merge 1 commit into
Canopus-Labs:mainfrom
sashatakpere:fix-country-field-validation
Open

fix: Validate country field accepting numeric input#1714
sashatakpere wants to merge 1 commit into
Canopus-Labs:mainfrom
sashatakpere:fix-country-field-validation

Conversation

@sashatakpere

@sashatakpere sashatakpere commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

📝 Pull Request Description

Related Issue

Closes #786

Summary

Fixed the Country field in the profile form to prevent numeric and invalid alphanumeric input.

Previously, the Country field accepted values such as 11111111 without validation. Validation has now been added so that the field accepts valid country-name characters while rejecting numeric and alphanumeric input.


Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature
  • ♻️ Refactoring
  • 📝 Documentation update
  • 🎨 UI/UX improvement
  • 🔥 Other(please describe) _____

How Has This Been Tested?

Describe the testing steps performed.

  • Tested valid country names such as India.
  • Tested country names containing spaces such as United States.
  • Tested numeric input such as 11111111 and confirmed it is rejected.
  • Tested alphanumeric input such as India123 and confirmed it is rejected.
  • Confirmed that valid country names can still be entered normally.

Screenshots (if applicable)

N/A


Checklist

  • My code follows the project's guidelines
  • I have tested my changes
  • I have updated documentation where necessary
  • I have linked the related issue
  • My changes do not introduce new warnings or errors

Summary

  • Updated Country field validation.
  • Accepts letters, spaces, apostrophes, and hyphens.
  • Ignores numeric and invalid alphanumeric input.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Country input now validates entered characters. It stores only letters, spaces, apostrophes, and hyphens. Invalid input is ignored.

Changes

Country validation

Layer / File(s) Summary
Country input filtering
frontend/src/pages/Settings/Settings.jsx
The Country field validates input before updating state. Numeric and other invalid characters are not stored.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: karanunique

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The change rejects numeric and alphanumeric input but does not validate actual country names or show the required validation message for issue #786. Validate the value against a country list and display an appropriate error message for invalid input; autocomplete remains optional.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the country field validation fix for numeric input.
Out of Scope Changes check ✅ Passed The changes are limited to country field validation and align with the requirements in issue #786.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/src/pages/Settings/Settings.jsx`:
- Line 711: Update the country-name validation in the Settings form to accept
Unicode letters while preserving spaces, apostrophes, and hyphens; replace the
ASCII-only pattern in the visible validation condition with Unicode-aware
matching or canonical country-list validation.
- Around line 705-717: Update the backend country handling in the auth
controller’s user-country assignment flow to validate req.body.country against a
canonical country-name rule or allowlist before persisting it. Reject invalid
values such as numeric-only, alphanumeric, or punctuation-only strings, and
ensure all clients receive the same validation behavior regardless of the
frontend filter.
- Around line 705-717: Update the country field and its form submission flow to
track validation state, display an accessible error message when the value is
empty or fails the existing country-character rule, and add the required
attribute. Ensure onChange records invalid input rather than silently retaining
the previous value, and block save until country.trim() is non-empty and valid.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b902a7a4-ebb6-4f55-aeed-9315dd3a9c36

📥 Commits

Reviewing files that changed from the base of the PR and between ca675c8 and d7f5850.

📒 Files selected for processing (1)
  • frontend/src/pages/Settings/Settings.jsx

Comment on lines 705 to 717
<input
type="text"
value={country}
onChange={(e) => setCountry(e.target.value)}
onChange={(e) => {
const value = e.target.value;

if (/^[A-Za-z\s'-]*$/.test(value)) {
setCountry(value);
}
}}
placeholder="Enter Country"
className="w-full bg-slate-50 dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-lg py-2.5 px-4 text-sm text-slate-900 dark:text-white"
/>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Enforce country validation on the API boundary.

This client-side filter does not protect persisted data. backend/controllers/authController.js:360-449 assigns req.body.country directly to user.country, so another client can store 11111111, India123, or ---. The regular expression also checks characters only. It does not verify that the value is a country name. Add server-side validation using one canonical country rule or country list.

🧰 Tools
🪛 ast-grep (0.45.0)

[warning] 711-711: Avoid using the initial state variable in setState
Context: setCountry(value)
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.

(setstate-same-var)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/Settings/Settings.jsx` around lines 705 - 717, Update the
backend country handling in the auth controller’s user-country assignment flow
to validate req.body.country against a canonical country-name rule or allowlist
before persisting it. Reject invalid values such as numeric-only, alphanumeric,
or punctuation-only strings, and ensure all clients receive the same validation
behavior regardless of the frontend filter.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Show an error and block save for invalid or empty country values.

The handler silently keeps the previous value when the user enters 11111111 or pastes India123. The input also has no required attribute. If the field starts empty, the form can submit an empty country without a validation message. Add validation state and an accessible error message, then reject save when country.trim() is empty or invalid.

🧰 Tools
🪛 ast-grep (0.45.0)

[warning] 711-711: Avoid using the initial state variable in setState
Context: setCountry(value)
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.

(setstate-same-var)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/Settings/Settings.jsx` around lines 705 - 717, Update the
country field and its form submission flow to track validation state, display an
accessible error message when the value is empty or fails the existing
country-character rule, and add the required attribute. Ensure onChange records
invalid input rather than silently retaining the previous value, and block save
until country.trim() is non-empty and valid.

onChange={(e) => {
const value = e.target.value;

if (/^[A-Za-z\s'-]*$/.test(value)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Allow Unicode letters in country names.

The [A-Za-z] pattern rejects valid names such as Côte d'Ivoire and São Tomé and Príncipe. Use Unicode-aware validation or a canonical country list while preserving spaces, apostrophes, and hyphens.

🧰 Tools
🪛 ast-grep (0.45.0)

[warning] 711-711: Avoid using the initial state variable in setState
Context: setCountry(value)
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.

(setstate-same-var)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/Settings/Settings.jsx` at line 711, Update the
country-name validation in the Settings form to accept Unicode letters while
preserving spaces, apostrophes, and hyphens; replace the ASCII-only pattern in
the visible validation condition with Unicode-aware matching or canonical
country-list validation.

@KaranUnique

Copy link
Copy Markdown
Contributor

@sashatakpere Coderabbit suggestions seems to be important, kindly take a look on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Country field accepts numeric input instead of validating country names

2 participants