Answers

Question and Answer:

  Home  Scripting

⟩ Explain how to validate website address using JavaScript? User should not be allowed to enter special characters except hyphen(-)?

function ValidateWebAddress(field,alerttext)

{

with(field)

{

var companyUrl =value;

var RegExp = /^(([w]+:)?//)?(([dw]|%[a-fA-fd]{2,2})+(:([dw]|%[a-fA-fd]{2,2})+)?@)?([dw][-dw]{0,253}[dw].)+[w]{2,4}(:[d]+)?(/([-+_~.dw]|%[a-fA-fd]{2,2})*)*(?(&?([-+_~.dw]|%[a-fA-fd]{2,2})=?)*)?(#([-+_~.dw]|%[a-fA-fd]{2,2})*)?$/;

i f(RegExp.test(companyUrl)) {

return true;

}

else

{

alert(alerttext);

return false;

}

}

}

Call this fuction in an if else structure

Pass the field name and an alert string

if(validate_r(website,"Need to specify your site")==false)

{

return false;

}

 255 views

More Questions for you: