如何在Web Service下的Web方法中设置验证以限制文本输入长度? [英] How to set validations in the web methods under Web Service to limit text entry length?

查看:115
本文介绍了如何在Web Service下的Web方法中设置验证以限制文本输入长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Web Service下的Web方法中设置验证以限制文本输入长度?例如,如果文本长度超过14个字符等。

How to set validations in the web methods under Web Service to limit text entry length? For instance if the text length is beyond 14 characters etc.

推荐答案

您可以从文本框修复



You can fix from textbox

<asp:textbox id="TextBox1" runat="server"  MaxLength="14" ></asp:textbox>


[WebMethod]
public string HelloWorld(string input)
{

   // you can throw new exception
   if (string.IsNullOrEmpty(input) || input.Length > 14)               
       throw new ArgumentException("Invalid Input");

   //Or you can limit the input without throwing exception  
   if (string.IsNullOrEmpty(input) &&  input.Length > 14)               
       input = input.Substring(0,14)

   // do something with input 
}


这篇关于如何在Web Service下的Web方法中设置验证以限制文本输入长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆