PDA

View Full Version : Modify a Line in a Script



stevan
May 7th, 2003, 03:08 AM
I am using the following Script

Is the a way to make sure the put numbers in the phone number box and check for 10 numbers>>

The line for this is not there so i need some help on the line that checks for it

<SCRIPT LANGUAGE="JavaScript">



<!-- Begin
function checkFields() {
missinginfo = "";
if (document.form.name.value == "") {
missinginfo += "\n - Name";
}
if ((document.form.from.value == "") ||
(document.form.from.value.indexOf('@') == -1) ||
(document.form.from.value.indexOf('.') == -1)) {
missinginfo += "\n - Email address";
}
if ((document.form.website.value == "") ||
(document.form.website.value.indexOf("http://") == -1) ||
(document.form.website.value.indexOf(".") == -1)) {
missinginfo += "\n - Web site";
}
if(document.form.comments.value == "") {
missinginfo += "\n - Comments";
}

if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>



THXS STEVE




:thumbsup:

HairyTeeth
May 7th, 2003, 07:38 AM
In keeping with your script (generally):


var phoneFld=document.form.phone;

if((isNaN(phoneFld.value)) || (phoneFld.value.length != 10)){
missinginfo+="\n - Phone Number"

Put the maxlength attribute in the text input as well:


<input type="text" maxlength="10" name="phone" />

stevan
May 7th, 2003, 01:06 PM
THXS very much

As a newbie this was one of the few scripts i could make work on my site

But the phone number part had me stumped

One other question? Is there a to format the phone number atomaticly so this show up

954/758/9856 or 987-987-9874

How would i change the script so whe they put in the 10 number is added the / or - automaticlly

Is this possible???



AGAIN THXS

STEVE:o

HairyTeeth
May 8th, 2003, 04:42 AM
I'd do something like this:


<html>
<head>
<title>Demo</title>

<script type="text/jav&#97;script" language="jav&#97;script">
<!--;

function validate(f){
var re = /\b\d{3}[-/]\d{3}[-/]\d{4}\b/
var theString = f.phone.value;

if(theString.search(re) == -1 || theString==""){
alert("Please check that you have entered a valid phone number.");
f.phone.focus();
f.phone.select();
return false;
}
if(f.email.value=="" || (f.email.value.indexOf("@")== -1)){
alert("Please check that you have entered a valid email address");
f.email.focus();
f.email.select();
return false;
}
return true;
}


function formatPhone(fld,delim){
var re =/(\d{3})(\d{3})(\d{4})/
var theString = fld.value;

if(fld.value.length !=10){
alert("Please enter 10 digits for the phone number");
document.frm.phone.focus();
document.frm.phone.select();
return false;
}
while(re.test(theString)){
theString=theString.replace(re,"$1"+delim+"$2"+delim+"$3");
}
fld.value = theString;
return true;
}

//-->
</script>

</head>
<body>

<form name="frm" action="jav&#97;script: alert('Success')"
method="post" onsubmit="return validate(this)">

<input type="text" name="phone" value="" maxlength="10"
onchange="formatPhone(this,'/')"/>
<br />

<input type="text" name="email" value="" maxlength="10" />
<br />

<input type="Submit" value="Submit" />
</form>

</body>
</html>

At the moment, you can use either "/" or "-" as the delimiters. To swap from "/" to the "-" (and visa-versa), change this line of code:

<input type="text" name="phone" value="" maxlength="10"
onchange="formatPhone(this,'/')"/>

to this:

<input type="text" name="phone" value="" maxlength="10"
onchange="formatPhone(this,'-')"/>


To add a new delimiter (say "|"), you will have to change this script statement:

var re = /\b\d{3}[-/]\d{3}[-/]\d{4}\b/

to this:

var re = /\b\d{3}[-/|]\d{3}[-/|]\d{4}\b/

This gobbledy gook looking stuff is regular expressions. Its a very powerful way of manipulating strings.

Philip M
May 8th, 2003, 05:03 AM
May I again make the point that compulsory format of telephone numbers to 10 digits will make it impossible for many users outside the USA to use your form. Obviously this may be what you want - or perhaps not?

If so you should say so on the form -

"Enter your phone number (must be 10 digits)"

HairyTeeth
May 8th, 2003, 06:03 AM
How would you make a regexp take into account variable string length and place the delimter at the appropriate point? My regexp ability isn't up to that. :(

EDIT: I know i could do this with a switch or if...else statements using. for example, string.length or % but, to clarify, i mean within the regexp itself. Possible?