/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] regular expression help

how would i make a regular expression to compare a number formated as:
w12345678

the first character HAS to be a w followed by 8 numbers 0-9
Thank you

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 21.2008 — [code=php]'/^[a-z]d{8}$/'[/code]
replace [B]a-z[/B] with [B]a-zA-Z[/B] for an incase sensitive version.

EDIT: what am I thinking, it's just w right?
[code=php]preg_match('/^wd{8}$/', $testMe);[/code]
Copy linkTweet thisAlerts:
@maitopoikaauthorNov 21.2008 — Thanks!

would it be the exact same if i want to also use it in javascript??
Copy linkTweet thisAlerts:
@scragarNov 21.2008 — just drop the quotes around it for javascript(or use the new RegExp('/^wd{8}$/') syntax).
Copy linkTweet thisAlerts:
@maitopoikaauthorNov 23.2008 — unfortunately I cannot get this regular expression to correctly validate the input

I know this is javascript at this point but this is where i am stuck, as i cannot get it to work in php either
<i>
</i>&lt;?php










?&gt;


&lt;!--Web page HTML--&gt;
&lt;html&gt;

&lt;head&gt;

&lt;style type="text/css"&gt;
h1.title {font-family:arial; color:#380070}
h2 {font-family:arial}
p {font-family:arial; margin-left:10 px}

&lt;/style&gt;


&lt;title&gt;CS 2350 Student Registration Page&lt;/title&gt;

&lt;script language="javascript"&gt;


<i> </i> function validate()
<i> </i> {

<i> </i> valid=true;
<i> </i> if(document.pageRegister.number.value =="" || !checkNumber(document.pageRegister.number.value))
<i> </i> {
<i> </i> alert("w number is incorrect");


<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> alert("yup"); // for testing purposes only

<i> </i> }
<i> </i> }


<i> </i> function checkNumber(str)
<i> </i> {
<i> </i> var wExp = /^wd{8}$/ ;
<i> </i> if(str.match(wExp))
<i> </i> {

<i> </i> return true;
<i> </i> }
<i> </i> else
<i> </i> {

<i> </i> return false;
<i> </i> }

<i> </i> }

<i> </i> function checkWnumber(str)
<i> </i> {
<i> </i> //will work on this part later



<i> </i> }


&lt;/script&gt;
&lt;/head&gt;

&lt;body bgcolor="#380070"&gt;
&lt;center&gt;






&lt;table width="1040" border="0" bgcolor="white" height="754" cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;&lt;td height="100" width="325"&gt;
&lt;img border="0" src="finalfiles/maintop.jpg" width="296" height="100"&gt;&lt;/td&gt;
&lt;td height="100" width="715"&gt;&lt;h1 class="title"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CS 2350 Internet Programming&lt;/h1&gt;&lt;/td&gt;&lt;/tr&gt;

<i> </i>&lt;tr&gt;&lt;td colspan="2" height="10"&gt;&lt;hr&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i>&lt;tr&gt;&lt;td colspan="2" height="78"&gt;&lt;center&gt;&lt;h2&gt;Student Web Page Registration&lt;/h2&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i>&lt;tr&gt;&lt;td colspan="3"height="97"&gt;

<i> </i> &lt;form name="pageRegister" id="pageRegister" method="post" onsubmit="return validate()"&gt;
<i> </i> &lt;p&gt;
<i> </i> Name &lt;input type="text" size="55" name="name" id="name"&gt;&lt;br/&gt;
<i> </i> W Number:&lt;input type="text" size="15" name="number" id="number"&gt;&lt;br/&gt;
<i> </i> WebSite Address http://&lt;input type="text" size="40" name="web" id="web"&gt;&lt;br/&gt;
<i> </i> &lt;input type="submit" value="submit"&gt;
<i> </i> &lt;/p&gt;


<i> </i> &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;

<i> </i>&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;


&lt;/table&gt;
&lt;/center&gt;
&lt;/body&gt;

&lt;/html&gt;



here is the whole page
Copy linkTweet thisAlerts:
@JickNov 23.2008 — [CODE]new RegExp('/^wd{8}$/')[/CODE][/QUOTE]I understand the [FONT="Courier New"]d{8}[/FONT] part. That should mean 8 digits. I also understand [FONT="Courier New"]^[/FONT] for the beginning of the string and [FONT="Courier New"]$[/FONT] for the end of the string but, the [FONT="Courier New"]w[/FONT] part is confusing me. It seems to me that the slash should go before the [FONT="Courier New"]w[/FONT] like [FONT="Courier New"]w[/FONT] which comments out the [FONT="Courier New"]w[/FONT] so the regex doesn't try to interpret it but rather leave it alone. But, the way you have it is [FONT="Courier New"]w[/FONT]... Is that something I'm not aware of or just a typo? Because if it is a typo then that is probably the reason it's not working for maitopoika.
Copy linkTweet thisAlerts:
@maitopoikaauthorNov 23.2008 — I got it.

Thank you all!!!
Copy linkTweet thisAlerts:
@SyCoNov 26.2008 —  the w part is confusing me. [/QUOTE]

Jick, the w is a literal match for the letter w which was part of the requirement in the OP matching requirement.

the first character HAS to be a w followed by 8 numbers 0-9[/QUOTE]
Copy linkTweet thisAlerts:
@MrCoderNov 27.2008 — [code=php]'/^[a-z]d{8}$/'[/code]
replace [B]a-z[/B] with [B]a-zA-Z[/B] for an incase sensitive version.

EDIT: what am I thinking, it's just w right?
[code=php]preg_match('/^wd{8}$/', $testMe);[/code][/QUOTE]


You can also do '/^[a-z]d{8}$/[B]i[/B]' for insensitive matching
×

Success!

Help @maitopoika spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...