正则表达式判断是否是合法路径


 

在页面前端使用一个文本框,输入条件是:c:test

 

在java中File判断是可以通过的,可以创建的

 

 

File file = new File(filePath);
if (!file.exists()) {
     file.mkdir();
} 

 

 

说明是一个合法的路径。

 

 

但是在windows中,如果打开我的电脑,然后输入c:test,会报错“windows找不到xxxx,请检查拼写并重试”

 

采用正在表达式可以解决这一问题:

 

代码如下:

 

function testPath() {
	var path = /^[a-zA-Z]:[\\]((?! )(?![^\\/]*\s+[\\/])[\w -]+[\\/])*(?! )(?![^.]*\s+\.)[\w -]+$/;
	var backuppath = document.getElementById("xxx");
	if (path.test(backuppath)) {
		return true;
	} else {
		alert("请填写正确路径!");
		return false;
	}
	return true;
}

你可能感兴趣的:(jQuery)