Skip to content

Commit 21a857c

Browse files
devin-ai-integration[bot]Alek99LineIndent
authored
Add comprehensive phone number validation to pricing demo form (#1452)
- Add regex-based phone validation supporting US and international formats - Implement validation error UI with red border and error message - Follow existing email validation pattern for consistency - Support formats: (555) 123-4567, +1-555-123-4567, 555.123.4567, international - Reject invalid formats: incomplete numbers, letters, too short/long numbers - Add banned_phone state variable for error handling - Set focus back to phone field on validation failure Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek <[email protected]> Co-authored-by: Ahmad Hakim <[email protected]>
1 parent 1bcd130 commit 21a857c

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

pcweb/pages/pricing/header.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import urllib.parse
23
from typing import Any, Literal
34

@@ -114,13 +115,29 @@ class QuoteFormState(rx.State):
114115
num_employees: str = "500+"
115116
referral_source: str = "Google Search"
116117
banned_email: bool = False
118+
banned_phone: bool = False
117119

118120
def set_select_value(self, field: str, value: str):
119121
"""Update the selected value for a given field."""
120122
setattr(self, field, value)
121123

122124
@rx.event
123125
def submit(self, form_data: dict[str, Any]):
126+
# Phone number validation
127+
phone = form_data.get("phone_number", "").strip()
128+
if phone:
129+
phone_pattern = r'^(\+?1[-.\s]?)?(\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4})|(\+[1-9]\d{0,3}[-.\s]?(\(?[0-9]{1,4}\)?[-.\s]?){1,5}[0-9]{1,4})$'
130+
131+
# Remove common formatting characters for validation
132+
clean_phone = re.sub(r'[^\d+]', '', phone)
133+
134+
if not re.match(phone_pattern, phone) or len(clean_phone) < 10 or len(clean_phone) > 15:
135+
self.banned_phone = True
136+
yield rx.set_focus("phone_number")
137+
return
138+
139+
self.banned_phone = False
140+
124141
# Email domain validation
125142
banned_domains = [
126143
"gmail.com",
@@ -377,12 +394,36 @@ def custom_quote_form() -> rx.Component:
377394
),
378395
class_name="flex-row flex gap-x-2 mb-6",
379396
),
380-
text_input_field(
381-
"Phone number",
382-
"phone_number",
383-
"(555) 123-4567",
384-
required=True,
385-
input_type="tel",
397+
rx.cond(
398+
QuoteFormState.banned_phone,
399+
rx.box(
400+
rx.el.div(
401+
rx.text(
402+
"Phone number",
403+
class_name="text-slate-11 text-sm font-medium mb-2",
404+
),
405+
rx.text(
406+
"Invalid phone number format!",
407+
class_name="text-red-8 text-sm font-medium mb-2",
408+
),
409+
class_name="flex flex-row items-center justify-between w-full",
410+
),
411+
rx.el.input(
412+
placeholder="Please enter a valid phone number",
413+
name="phone_number",
414+
type="tel",
415+
required=True,
416+
class_name="box-border w-full border-2 border-red-5 bg-slate-1 px-6 pr-8 border rounded-[0.625rem] h-[2.25rem] font-medium text-slate-12 text-sm placeholder:text-slate-9 outline-none focus:outline-none caret-slate-12 peer pl-2.5 disabled:cursor-not-allowed disabled:border disabled:border-slate-5 disabled:!bg-slate-3 disabled:text-slate-8 disabled:placeholder:text-slate-8",
417+
),
418+
class_name="mb-6",
419+
),
420+
text_input_field(
421+
"Phone number",
422+
"phone_number",
423+
"(555) 123-4567",
424+
required=True,
425+
input_type="tel",
426+
),
386427
),
387428
# Project Details
388429
textarea_field(

0 commit comments

Comments
 (0)