Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating first character in a form text field

Status
Not open for further replies.

bamboo

Programmer
Aug 22, 2001
89
US
I'm trying to validate that the first character in a form text field is not a letter. This is for a Cold Fusion application using type="file". However I do not want them to browse to a document stored in a letter drive on their machine, but rather a file share mapped in My Network Places. (C:\ vs \\fil). The js will do an easy check on the first character in the form field. If it starts with a letter then it will alert them.
 
If you want to make sure it starts with \\sharename and that you are happy with checking the starting character, I would then check only "\\" part like this.
[tt]
function checkit(s) {
if (!/^\\\\/.test(s)) {
alert("Only unc path is allowed.")
}
}
[/tt]
It can be called like these cases.
[tt]
<input type="file" onchange="checkit(this.value)" />
//or
<input type="text" onblur="checkit(this.value) />
[/tt]
As to what to do after invalid entries are made, you have to decide for yourself. Probably at the level of onsubmit handler of the form itself.
 
This works great. Is there a way I can clear the text field and do a focus after the alert? I've tried a couple different things, but they don't seem to work.

function checkit(s) {
if (!/^\\\\/.test(s)) {
alert("Only unc path is allowed.")
document.myForm.docURL.focus();
document.myForm.docURL.value = '';
}
}
 
Sure you can on text input element, but not on file input element due to the security infrastructure. To get this done, it would be better to pass the input element object itself. In order to do it successfully cross-browser, the simply device is to add a global variable, gobj say, like this.
[tt]
var gobj;
function checkit2(obj) {
if (!/^\\\\/.test(obj.value) {
alert("Only unc path is allowed.")
if (obj.type!="file") {
//only if obj is not type="file" which will have no effect and provoke security
document.obj.value = "";
}
gobj=obj;
setTimeout("gobj.focus()",20);
}
}
[/tt]
And called it like this.
[tt]
<input type="file" onchange="checkit2(this)" />
<!-- or -->
<input type="text" onblur="checkit2(this)" />
[/tt]
 
Amendment

The corresponding line should be read like this.
[tt]
obj.value = ""; //without document part which is a leftover
[/tt]
 
Thanks Tsuji! I really appreciate it. I'll try this out today and let you know how it goes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top